-- 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:
> 
> <input type="text" name="lastName" id="lastName" value="" size="30" />
> 
> Instead of this:
> 
> <input type="text" name="person[lastName]" id="person-lastName" value="" />
> 
> 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/

Reply via email to