ZF-3304: SubForms and their clones cannot always be modified separately
Given the ZF trunk at revision 9496, cloned Zend_Form_SubForm objects behave rather unexpectedly. Specifically, it seems impossible to modify certain properties or elements of the clone without also modifying the original, and/or vice versa.
Consider the following reproduce code:
$form = new Zend_Form(); $form->setView(new Zend_View()); // Create the original SubForm $subform1 = new Zend_Form_SubForm(); $subform1->addElement('text', 'given_name') ->addElement('text', 'family_name') ->setLegend('Me'); $form->addSubForm($subform1, 'me'); // Create a clone of the original SubForm and change it up a bit $subform2 = clone $subform1; $subform2->setLegend('You'); $form->addSubForm($subform2, 'you'); // Populate the form with simulated $_POST values $postedValues = array(); $postedValues['me']['given_name'] = 'Adam'; $postedValues['me']['family_name'] = 'Jensen'; $postedValues['you']['given_name'] = 'Someone'; $postedValues['you']['family_name'] = 'Else'; $form->populate($postedValues); // Output the finished product; // the values and legends for both forms should be different, // as they were set differently above. echo $form;
The actual result looks like this (here and in the expected result, I've added some line breaks and indents for readability):
Notice that the legends of both subforms are from $subform1 ("Me"), while the element values in both subforms are from $subform2 ("Someone" and "Else"). This contradicts the expected result below:
So...in the actual result, the values for the "me" subform are those assigned to the "you" subform, while the legend for the "you" subform is that assigned to the "me" subform. In a nutshell, we seem to have an identity crisis.
(By the way...I attempted to post this (twice) to the fw-mvc mailing list for community review, but my posting never showed up (either time). So, please forgive me if there's an easy solution to this problem that I would have found had my message gotten through to the mailing list :) )