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:
<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?
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.