Colin J wrote:
> 
> I was wondering if anyone can help with how to render a single sub form
> element in a view script.
> 
> I have a Zend_Form_SubForm and I am attaching this same sub form a number
> of times in the Controller, to a Zend_Form, e.g.
> 
> $form = new Zend_Form();
> 
> for ($i = 1; $i <= 5; ++$i) {
>       $mysubform = new MySubForm();
>       $form->addSubForm($mysubform, 'mysubformname' . $i);
> }
> 
> In the view script, if I render the parent form <?php echo $this->form ?>
> I get the following named elements in the sub form:
> 
> <input name="mysubformname1[elementName]"/>
> <input name="mysubformname2[elementName]"/>
> 
> Which is fine and all is well with the form handling, validation etc.
> However, I would like to render the elements individually in the view
> script because I need to lay them out in a certain way (too complex for me
> to use decorators at the moment).  If I was rendering the Zend_Form
> elements I would use:
> 
> <?php echo $this->form->elementName ?>
> 
> But, what do I use for the sub form?  If I use this:
> 
> <?php echo $this->form->getSubForm('mysubformname1')->elementName ?>
> <?php echo $this->form->getSubForm('mysubformname2')->elementName ?>
> or
> <?php echo $this->form->mysubformname1->elementName ?>
> 
> I just get (for all):
> 
> <input name="elementName">
> 
> Any help would be much appreciated.
> 
> Thanks
> 
> Colin
> 


For anyone looking for the answer to this question, I think I have stumbled
across the answer in the documentation.  I needed to set the
'PrepareElements' decorator before handing over to the viewScript.  This
ensures my subform elements now have the array notation I was expecting.  As
an example, I have setup the following decorators:

$this->setDecorators(
        array(
                'PrepareElements',
                array('viewScript', array('viewScript' =>
'decorators/myviewscript.phtml'))
        )
);

And in my view script I can call:

echo $this->element->elementName;

This is mentioned in the documentation here 
http://http://framework.zend.com/manual/en/zend.form.standardDecorators.html
Zend Framework Decorators  (see Zend_Form_Decorator_PrepareElements)
-- 
View this message in context: 
http://www.nabble.com/Rendering-a-sub-form-element-in-a-view-script-tp21750939p21790251.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to