Re: [fw-general] Zend_Form ViewScript Elements don't show

2008-11-05 Thread Matthew Weier O'Phinney
-- dbroderick <[EMAIL PROTECTED]> wrote
(on Wednesday, 05 November 2008, 02:38 PM -0800):
> I am relatively new to ZF (currently using 1.6.2) and am working on a
> multi-page form (subforms) and need to have one of my pages use the
> ViewScript so that I could write custom HTML for the layout.
> 
> I found this post response that helped me in getting my subform partially
> working, one key bit of code I was missing was what you provided here:
> 
> 
> 
> > Make sure that you call setView() on the elements. Easiest would be to
> > do the following at the top of your view script:
> > 
> > foreach ($this->element as $item) {
> > $item->setView($this);
> > }
> > 
> 
> However, my subform validation was not working (basically anything I
> submitted whether valid or not would break validation) and so I dug into the
> ZF code to compare the Zend_Form_Decorator_FormElements render function with
> the Zend_Form_Decorator_ViewScript render function.
> 
> What I noticed when I looked at the generated source code using the
> ViewScript is that my elements did not have the proper assignment to the
> subform.
> 
> I noticed my form elements under viewscript rendered like this:
> 
> 
> 
> Instead of this:
> 
> 
> 
> My fix was to make a copy of the ViewScript class, add the following code to
> it and set my decorator for that subform with my custom ViewScript.
> 
> /*
>  * Added to retain belongsto info for use in subforms
>  */
> $belongsTo  = ($element instanceof Zend_Form) ?
> $element->getElementsBelongTo() : null;
> $translator = $element->getTranslator();
>   
> foreach ($element as $item) {
>   $item->setView($view)
>->setTranslator($translator);
>   if ($item instanceof Zend_Form_Element) {
>   $item->setBelongsTo($belongsTo);
>   } elseif (!empty($belongsTo) && ($item instanceof Zend_Form)) {
>   if ($item->isArray()) {
>   $name = $this->_mergeBelongsTo($belongsTo, 
> $item->getElementsBelongTo());
>   $item->setElementsBelongTo($name, true);
>   } else {
>   $item->setElementsBelongTo($belongsTo, true);
>   }
>   } elseif (!empty($belongsTo) && ($item instanceof 
> Zend_Form_DisplayGroup))
> {
>   foreach ($item as $element) {
>   $element->setBelongsTo($belongsTo);
>   }
>   }
> }
> 
> The actual question I have here is, shouldn't this code or something like it
> already be a part of the current ViewScript class so that ViewScript can
> support subform validation?

Well, it can't be part of ViewScript, as ViewScript can be used with
individual elements as well as forms. However, this would be a good
candidate for a form decorator; you could then do this:

$form->setDecorators(array(
'PrepareElements',
array('ViewScript', array('form.phtml'))
);

I'm adding a request to the tracker for this improvement; I think it
will help a lot of people. The issue URL is:

http://framework.zend.com/issues/browse/ZF-4812

-- 
Matthew Weier O'Phinney
Software Architect   | [EMAIL PROTECTED]
Zend Framework   | http://framework.zend.com/


Re: [fw-general] Zend_Form ViewScript Elements don't show

2008-11-05 Thread dbroderick

Matthew,

I am relatively new to ZF (currently using 1.6.2) and am working on a
multi-page form (subforms) and need to have one of my pages use the
ViewScript so that I could write custom HTML for the layout.

I found this post response that helped me in getting my subform partially
working, one key bit of code I was missing was what you provided here:



> Make sure that you call setView() on the elements. Easiest would be to
> do the following at the top of your view script:
> 
> foreach ($this->element as $item) {
> $item->setView($this);
> }
> 

However, my subform validation was not working (basically anything I
submitted whether valid or not would break validation) and so I dug into the
ZF code to compare the Zend_Form_Decorator_FormElements render function with
the Zend_Form_Decorator_ViewScript render function.

What I noticed when I looked at the generated source code using the
ViewScript is that my elements did not have the proper assignment to the
subform.

I noticed my form elements under viewscript rendered like this:



Instead of this:



My fix was to make a copy of the ViewScript class, add the following code to
it and set my decorator for that subform with my custom ViewScript.

/*
 * Added to retain belongsto info for use in subforms
 */
$belongsTo  = ($element instanceof Zend_Form) ?
$element->getElementsBelongTo() : null;
$translator = $element->getTranslator();

foreach ($element as $item) {
$item->setView($view)
 ->setTranslator($translator);
if ($item instanceof Zend_Form_Element) {
$item->setBelongsTo($belongsTo);
} elseif (!empty($belongsTo) && ($item instanceof Zend_Form)) {
if ($item->isArray()) {
$name = $this->_mergeBelongsTo($belongsTo, 
$item->getElementsBelongTo());
$item->setElementsBelongTo($name, true);
} else {
$item->setElementsBelongTo($belongsTo, true);
}
} elseif (!empty($belongsTo) && ($item instanceof 
Zend_Form_DisplayGroup))
{
foreach ($item as $element) {
$element->setBelongsTo($belongsTo);
}
}
}

The actual question I have here is, shouldn't this code or something like it
already be a part of the current ViewScript class so that ViewScript can
support subform validation?

David

-- 
View this message in context: 
http://www.nabble.com/Zend_Form-ViewScript-Elements-don%27t-show-tp19696062p20350492.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Zend_Form ViewScript Elements don't show

2008-09-26 Thread devxtech

Here is what Matthew found out to be the problem. Just posting the solution
for anyone else who has my same issue.



Matthew Weier O'Phinney-3 wrote:
> 
>> I forgot to include that at the top of my viewscript before was the
>> following code:
>>
>> foreach ($this->element as $element):
>>  $element->setView($this);
>> endforeach;
>>
>> I changed it to what you suggested and still no elements are being
>> displayed.
>>
>> At the top of my viewscript now is:
>>
>> foreach ($this->element as $element) {
>>  $element->setView($this);
>>  }
>>
>> Also the getAction() and getMethod() don't output any data.
> 
> Figured it out. You're using:
> 
>element->firstName ?>
> 
> instead of :
> 
>element->firstName ?>
> 
> The first doesn't echo anything. ;)
> 
-- 
View this message in context: 
http://www.nabble.com/Zend_Form-ViewScript-Elements-don%27t-show-tp19696062p19696735.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Zend_Form ViewScript Elements don't show

2008-09-26 Thread Matthew Weier O'Phinney
-- devxtech <[EMAIL PROTECTED]> wrote
(on Friday, 26 September 2008, 01:43 PM -0700):
> 
> After looking through the mailing list archives and trying a lot of different
> things I can't seem to find out why the form elements won't display from my
> viewscript. They display fine when i don't use a viewscript and use the
> default decorators.

Make sure that you call setView() on the elements. Easiest would be to
do the following at the top of your view script:

foreach ($this->element as $item) {
$item->setView($this);
}


> My viewscript (/application/views/scripts/contactForm.phtml) looks like
> this:
> 
> Please Fill Me Out
> 
> 
> Contact Information
> Please provide us with the following information so we may contact
> you.
> element->firstName; ?>
> element->lastName; ?>   
> element->email; ?>
> element->telephoneNum; ?>
> 
> 
> Contact Reason & Message
> Now please provide us with your reason for contacting us and a brief
> message.
> element->contactReason; ?>
> element->message; ?>
> 
> element->submit; ?>
> 
> 
> My class in (/application/forms/ContactForm.php) is as follows:
> 
> class forms_ContactForm extends Zend_Form {
>   public function __construct($options = null)
>   {
>   parent::__construct($options);
>   $this->setAction('');
>   $this->setMethod('post');
>   $this->setName('contact_us');
>   
>   $firstName = new Zend_Form_Element_Text('firstName');
>   $firstName->setLabel('First Name')
> ->setRequired(true)
> ->addValidator('NotEmpty', true)
> ->addValidator('Alpha', true);
>   
>   $lastName = new Zend_Form_Element_Text('lastName');
>   $lastName->setLabel('Last Name')
>->setRequired(true)
>->addValidator('NotEmpty')
>->addValidator('Alpha', true);
>
>   $telephoneNum = new Zend_Form_Element_Text('telephoneNum');
>   $telephoneNum->setLabel('Telephone Number')
>//->setDescription('example 
> (555)555-')
>->setRequired(false)
>->addValidator('NonEmpty');
>
>   $email = new Zend_Form_Element_Text('email');
>   $email->setLabel('Email Address')
> ->addFilter('StripTags')
> ->addFilter('StringTrim')
> ->addFilter('StringToLower')
> ->setRequired(true)
> ->addValidator('NotEmpty', true)
> ->addValidator('EmailAddress');
> 
>   $contactReason = new Zend_Form_Element_Select('contactReason');
>   $contactReason->setLabel('Reason for Contact')
> ->setRequired(true)
> ->addMultiOption('none','Please 
> Select One')
> 
> ->addMultiOption('personal','Personal')
> 
> ->addMultiOption('business','Business')
> ->addValidator('NotEmpty');
>   
>   $message = new Zend_Form_Element_Textarea('message');
>   $message->setLabel('Comments')
>   ->setAttrib('rows','15')
>   ->setAttrib('cols','70')
>   ->setRequired(true)
>   ->addValidator('NotEmpty');
>   
>   $submit = new Zend_Form_Element_Submit('submit');
>   $submit->setLabel('Submit Form');
>   
>   $this->setDecorators(array(
>   array('ViewScript',array('viewScript' => 
> 'forms/contactForm.phtml'))
>   ));
>   
>   $this->addElements(array($firstName, $lastName, $email, 
> $telephoneNum,
> $contactReason, $message, $submit));
>   }
> }
> ?>
> 
> 
> When the script runs it outputs the fieldsets and the legends with the
> descriptions but nothing else is outputted. Any ideas on what could be
> causing this. Been trying to figure this out for a few days now and could
> really use some help as I'm still learning OOP and the Zend Framework.
> -- 
> View this message in context: 
> http://www.nabble.com/Zend_Form-ViewScript-Elements-don%27t-show-tp19696062p19696062.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
> 

-- 
Matthew Weier O'Phinney
Software Architect   | [EMAIL PROTECTED]
Zend Framework   | http://framework.zend.com/


[fw-general] Zend_Form ViewScript Elements don't show

2008-09-26 Thread devxtech

After looking through the mailing list archives and trying a lot of different
things I can't seem to find out why the form elements won't display from my
viewscript. They display fine when i don't use a viewscript and use the
default decorators.

My viewscript (/application/views/scripts/contactForm.phtml) looks like
this:

Please Fill Me Out


Contact Information
Please provide us with the following information so we may contact
you.
element->firstName; ?>
element->lastName; ?> 
element->email; ?>
element->telephoneNum; ?>


Contact Reason & Message
Now please provide us with your reason for contacting us and a brief
message.
element->contactReason; ?>
element->message; ?>

element->submit; ?>


My class in (/application/forms/ContactForm.php) is as follows:

class forms_ContactForm extends Zend_Form {
public function __construct($options = null)
{
parent::__construct($options);
$this->setAction('');
$this->setMethod('post');
$this->setName('contact_us');

$firstName = new Zend_Form_Element_Text('firstName');
$firstName->setLabel('First Name')
  ->setRequired(true)
  ->addValidator('NotEmpty', true)
  ->addValidator('Alpha', true);

$lastName = new Zend_Form_Element_Text('lastName');
$lastName->setLabel('Last Name')
 ->setRequired(true)
 ->addValidator('NotEmpty')
 ->addValidator('Alpha', true);
 
$telephoneNum = new Zend_Form_Element_Text('telephoneNum');
$telephoneNum->setLabel('Telephone Number')
 //->setDescription('example 
(555)555-')
 ->setRequired(false)
 ->addValidator('NonEmpty');
 
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email Address')
  ->addFilter('StripTags')
  ->addFilter('StringTrim')
  ->addFilter('StringToLower')
  ->setRequired(true)
  ->addValidator('NotEmpty', true)
  ->addValidator('EmailAddress');
  
$contactReason = new Zend_Form_Element_Select('contactReason');
$contactReason->setLabel('Reason for Contact')
  ->setRequired(true)
  ->addMultiOption('none','Please 
Select One')
  
->addMultiOption('personal','Personal')
  
->addMultiOption('business','Business')
  ->addValidator('NotEmpty');

$message = new Zend_Form_Element_Textarea('message');
$message->setLabel('Comments')
->setAttrib('rows','15')
->setAttrib('cols','70')
->setRequired(true)
->addValidator('NotEmpty');

$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Submit Form');

$this->setDecorators(array(
array('ViewScript',array('viewScript' => 
'forms/contactForm.phtml'))
));

$this->addElements(array($firstName, $lastName, $email, 
$telephoneNum,
$contactReason, $message, $submit));
}
}
?>


When the script runs it outputs the fieldsets and the legends with the
descriptions but nothing else is outputted. Any ideas on what could be
causing this. Been trying to figure this out for a few days now and could
really use some help as I'm still learning OOP and the Zend Framework.
-- 
View this message in context: 
http://www.nabble.com/Zend_Form-ViewScript-Elements-don%27t-show-tp19696062p19696062.html
Sent from the Zend Framework mailing list archive at Nabble.com.