ZF-10007: Add all parents to form element name/id optionally when outputting directly
Description
Text Element: items[410412345][fieldname] Works perfect if I am just rendering the form normally (ie. echo $this->form; ) This time around though, I'm doing some more complicated stuff with the form and wanted to control exactly where and how the elements are rendered, so I thought I'd echo the subforms/elements directly in my view script.
<?php echo $this->form->items; ?>
works
outputs
Comments
Posted by Scott Morken (ss10sb) on 2010-06-17T12:01:37.000+0000
I see now that there is a way to do this using elementsBelongTo and belongsTo (ZF-9451).
Posted by Christian Albrecht (alab) on 2010-06-17T12:44:14.000+0000
That would require Zend_Form_SubForm to have a belongsTo member as well, that is not the case atm. What is the way you see it?
Posted by Scott Morken (ss10sb) on 2010-06-17T13:08:33.000+0000
I would love to see that. I have a deeply nested set of elements that I finally got to name correctly using the elementsBelongTo property on their immediate subform parent, but that is making for some interesting returns in ::getValues($_POST) since Zend_Form is seeing multiple belongTos and nesting them oddly in the merge - element is getValues is merging the subForms' belongTos on top of array so that I end up with something like:
I was planning to override getValues in my extended form and see if I can get it to drop the extra nesting.
With that in mind, I think adding an option to iterate over all the parents would be great. The idea seems easier than what I'm doing above (elementsBelongTo and overriding the ::getValues() to fix the array). But you know this thing inside and out and know what kind of mess it would make to add that feature.
Posted by Scott Morken (ss10sb) on 2010-06-17T15:50:38.000+0000
I thought about what you said about adding a belongsTo member to SubForm and decided to give that a shot since my attempt at overriding ::getValues() above was turning into a bigger mess than it was worth. I can guarantee I've missed some obvious things/bugs/disasters in what I did, but I thought I'd pass it on and if there is anything you can use, have at it! I decided to go with 'parent' as the subForm member and used it to modify the belongsTo properties as needed. It basically grabs the parent of the previous form/subform and adds it to the current subform + the current's parent, a simple array. I extended Zend_Form and Zend_Form_SubForm (only so I could point it at my new Form class so it could pick up the parent property). I made a small change to Zend_Form_Decorator_FormElements::render method as follows:
My extended files are attached. Yes I know, "PC" isn't Zend legal ;) I know there will be a lot more to it than what I've done here (like adding the toggle to use all the parents or not for multipage), but I hope it is in the right direction!
Posted by Scott Morken (ss10sb) on 2010-06-17T15:51:44.000+0000
PC_Form and PC_Form_SubForm files
Posted by Scott Morken (ss10sb) on 2010-06-17T16:02:28.000+0000
Oops, overriding _setElementsBelongTo() causes double nesting. Leaving it as is seems to work perfectly.
Posted by Christian Albrecht (alab) on 2010-06-30T14:24:55.000+0000
Postponing, should have for 2.0
Posted by Scott Morken (ss10sb) on 2010-09-23T07:41:45.000+0000
Some updated files (finally got around to extending the Zend_Form_Decorator_FormElements class instead of my little hack to the actual Zend file... laziness and procrastination!). Added a flag to set whether to use the parent form's in the array naming or not (::setUseParent(bool)). If it is set, it will use the parent's names in the array naming scheme. If not, it will use only the item being rendered as the parent.
There is a problem with it rendering parents correctly when rendering an entire form (<?php echo $this->form; ?> versus rendering the elements through a viewscript (<?php echo $this->element->elementname; ?>). The viewscript rendered elements pick up the parents correctly while the rendered form seems to drop the nesting above the form being rendered. I haven't had time to look into it and figure out why since I have been using a viewscript when I need the deep nesting anyway. (more laziness and procrastination)
I thought I would add my 'new' files in case they can help out with building 2.0.
Posted by Scott Morken (ss10sb) on 2010-09-23T07:44:51.000+0000
Oops, I guess I can't upload files anymore!