Search code examples
zend-frameworkzend-formzend-form-sub-form

Using ViewScript Decorator on Nested Subforms (Zend Form)


I want to use a view script to render my zend form as it seems to be the best way to control the layout/design of the form while still using the Zend_Elements classes.

From the view script, I render the element with $this->element->getElement('elementName') .

I'm having problems with the names of the elements. This is actually a sub-form inside a sub-form inside a form.

When I used the FormElements decorators , the fully qualified name of the elements was form[subForm][subForm][element] , which was good. Wehn I moved to the viewScript decorators, it changed to subForm[subForm][element].

I understood that I need to use the PrepareElements decorator to fix this, but this caused the name to change form[subForm][form][subForm][subForm][elements] (it doubled the first two names in the start).

Any ideas how I should handle this?

Thanks.

UPDATE: I tried to debug PrepareElements and I really don't understand what is doing. It seems like it works ok in the first iteration, but then it adds again the form[subform] prefix when running on one of the middle subforms.

When I'm not using the PrepareElements decorator, I'm just missing the "form" prefix in the names (i.e., instead of form[subForm][element], I get only subForm[element]).

May be I can just fix this somehow?

I tried to change the belongsTo but that only replaced the "subForm" prefix .

It actually seems like what is missing is a belongsTo method on the subForm.

Again, this is all because of the ViewScript decorator. It works fine with the FormElements decorators.

UPDATE 2: Just to clarify, I wouldn't mind this name change, but it causes my fields to not populate when I call form->populate .

Edit: I think that I've narrowed the problem to this: when I get my values back in setDefaults, they are ordered like this:

array(
\"formElements1-name\" => value1... \"subFormName\" => array(
\"parentFormName\" => array(
\"subFormName\" => subForm-values-array
)
)

... The main problem here is the "parentFormName" => "subFormNAme".. what does it repeat itself? I'm already in the main form. I'm guessing this is caused because I've set the setElementsBelongTo(formName[subFormName]) , but if I wouldn't do that, then I would get my subform values completely separate from the form,

i.e. values array = array( \"formName\" => array( formValues ), \"subFormNAme\" => array( subFormValues )

, while I exepct it to be

array(
formName => array(
subFormNAme => values-array
)
)...

Is it even possible to make this work?


Solution

  • The current solution is to use the PrepareElements decorator on the subforms with one change - remove the recursive call in the PrepareElements code. Also, no "setElementsBelongTo" is required.

    This seem to generate the correct names and ids.