Issues

ZF-9666: Forms with SubForms Having Elements with Same Names Populate Incorrectly

Issue Type: Patch Created: 2010-04-12T16:32:28.000+0000 Last Updated: 2010-04-16T07:32:50.000+0000 Status: Resolved Fix version(s): Reporter: Ewan Edwards (eskwayrd) Assignee: Christian Albrecht (alab) Tags: - Zend_Form

Related issues: Attachments:

Description

The following code:

$form = new Zend_Form; $form->addElement('text', 'foo'); $subForm = new Zend_Form_SubForm; $subForm->addElement('text', 'foo'); $form->addSubForm($subForm, 'mySubForm'); $form->populate(array('foo' => 'bar')); echo $form;

Renders as:

    The sub form's 'foo' should not be populated with 'bar'.

Comments

Posted by Christian Albrecht (alab) on 2010-04-13T01:13:46.000+0000

This patch fixes the issue, unit test included

<pre class="highlight">
Index: tests/Zend/Form/FormTest.php
===================================================================
--- tests/Zend/Form/FormTest.php        (Revision 21845)
+++ tests/Zend/Form/FormTest.php        (Arbeitskopie)
@@ -1184,6 +1184,21 @@
         $this->assertTrue($this->form->isValid($data));
     }
 
+    /**
+     * @group ZF-9666
+     */
+    public function testSetDefaultsDiscardsPopulatedValues()
+    {
+        $this->form->addElement('text', 'foo');
+        $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
+                   ->bar->addElement('text', 'foo');
+
+        $this->form->populate(array('foo' => 'foo Value'));
+        $html = $this->form->setView($this->getView())
+                           ->render();
+        $this->assertEquals(1, preg_match_all('/foo Value/', $html, $matches));
+    }
+
     // Sub forms
 
     public function testCanAddAndRetrieveSingleSubForm()
Index: library/Zend/Form.php
===================================================================
--- library/Zend/Form.php       (Revision 21845)
+++ library/Zend/Form.php       (Arbeitskopie)
@@ -1232,6 +1232,7 @@
         foreach ($this->getElements() as $name => $element) {
             if (array_key_exists($name, $defaults)) {
                 $this->setDefault($name, $defaults[$name]);
+                unset($defaults[$name]);
             }
         }
         foreach ($this->getSubForms() as $name => $form) {



<pre class="highlight">
    public function testSetDefaultsDiscardsPopulatedValues()
    {
        $this->form->addElement('text', 'foo');
        $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
                   ->bar->addElement('text', 'foo');

        $this->form->populate(array('foo' => 'foo Value'));
        $html = $this->form->setView($this->getView())
                           ->render();
        $this->assertEquals(1, preg_match_all('/foo Value/', $html, $matches));
    }

Posted by Ewan Edwards (eskwayrd) on 2010-04-13T14:44:30.000+0000

This patch appears to solve the problem. Thanks for attending to this so quickly!

Posted by Christian Albrecht (alab) on 2010-04-16T07:32:50.000+0000

This Patch duplicates now Patch #4 of ZF-9607

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts