ZF-9593: Zend_Form getMessages() invalid handling of !$subform->isArray()

Description

This is basically the same as the the previous Patches, [ZF-9586], [ZF-9467], [ZF-9584]

i cleaned the method as well for speed.

Uncleaned Before Patch

        if ((null !== $name) && isset($this->_elements[$name])) {
            return $this->getElement($name)->getMessages();
        }

        if ((null !== $name) && isset($this->_subForms[$name])) {
            return $this->getSubForm($name)->getMessages(null, true);
        }

        $arrayKeys = array();
        foreach ($this->getSubForms() as $key => $subForm) {
            $array = $this->_getArrayName($subForm->getElementsBelongTo());
            if (!empty($array)) {
                if ($name == $array) {
                    return $subForm->getMessages(null, true);
                }
                $arrayKeys[$key] = $subForm->getElementsBelongTo();
            }
        }

Cleaned After Patch

        if (null !== $name) {
            if (isset($this->_elements[$name])) {
                return $this->getElement($name)->getMessages();
            } else if (isset($this->_subForms[$name])) {
                return $this->getSubForm($name)->getMessages(null, true);
            }
            foreach ($this->getSubForms() as $key => $subForm) {
                if ($subForm->isArray()) {
                    $belongTo = $subForm->getElementsBelongTo();
                    if ($name == $this->_getArrayName($belongTo)) {
                        return $subForm->getMessages(null, true);
                    }
                }
            }
        }

The relevant changes for !$subForm->isArray() and interferences between Name and elementsBelongTo chain

Before Patch


         foreach ($this->getSubForms() as $key => $subForm) {
            $fMessages = $subForm->getMessages(null, true);
            if (!empty($fMessages)) {
                if (array_key_exists($key, $arrayKeys)) {
                    $fMessages = $this->_attachToArray($fMessages, $arrayKeys[$key]);
                    $messages = array_merge($messages, $fMessages);
                } else {
                    $messages[$key] = $fMessages;
                }
         }

And After Patch


         foreach ($this->getSubForms() as $key => $subForm) {
            $merge = $subForm->getMessages(null, true);
            if (!empty($merge)) {
                if (!$subForm->isArray()) {
                    $merge = array($key => $merge);
                } else {
                    $messages[$key] = $fMessages;
                    $merge = $this->_attachToArray($merge,
                                                   $subForm->getElementsBelongTo());
                }
                $messages = array_merge_recursive($messages, $merge);
             }
         }

And finally the patch


Index: tests/Zend/Form/FormTest.php
===================================================================
--- tests/Zend/Form/FormTest.php    (Revision 21732)
+++ tests/Zend/Form/FormTest.php    (Arbeitskopie)
@@ -1579,7 +1620,16 @@
         $this->assertSame($this->form->getValidValues($data['invalid']), $data['partial']);
     }
 
+    public function testGetMessagesWithElementsBelongTo()
+    {
+        $data = $this->_setup9350();
+        $this->form->isValid($data['invalid']);
+        $msgs = $this->form->getMessages();
+        $this->assertTrue(isset($msgs['foo']['foo']['foo']['foo']));
+        $this->assertTrue(isset($msgs['foo']['zoo']['iek']));
+    }
 
+
     // Display groups
 
     public function testCanAddAndRetrieveSingleDisplayGroups()
Index: library/Zend/Form.php
===================================================================
--- library/Zend/Form.php   (Revision 21732)
+++ library/Zend/Form.php   (Arbeitskopie)
@@ -2299,22 +2299,19 @@
      */
     public function getMessages($name = null, $suppressArrayNotation = false)
     {
-        if ((null !== $name) && isset($this->_elements[$name])) {
-            return $this->getElement($name)->getMessages();
-        }
-
-        if ((null !== $name) && isset($this->_subForms[$name])) {
-            return $this->getSubForm($name)->getMessages(null, true);
-        }
-
-        $arrayKeys = array();
-        foreach ($this->getSubForms() as $key => $subForm) {
-            $array = $this->_getArrayName($subForm->getElementsBelongTo());
-            if (!empty($array)) {
-                if ($name == $array) {
-                    return $subForm->getMessages(null, true);
+        if (null !== $name) {
+            if (isset($this->_elements[$name])) {
+                return $this->getElement($name)->getMessages();
+            } else if (isset($this->_subForms[$name])) {
+                return $this->getSubForm($name)->getMessages(null, true);
+            }
+            foreach ($this->getSubForms() as $key => $subForm) {
+                if ($subForm->isArray()) {
+                    $belongTo = $subForm->getElementsBelongTo();
+                    if ($name == $this->_getArrayName($belongTo)) {
+                        return $subForm->getMessages(null, true);
+                    }
                 }
-                $arrayKeys[$key] = $subForm->getElementsBelongTo();
             }
         }
 
@@ -2333,14 +2330,15 @@
         }
 
         foreach ($this->getSubForms() as $key => $subForm) {
-            $fMessages = $subForm->getMessages(null, true);
-            if (!empty($fMessages)) {
-                if (array_key_exists($key, $arrayKeys)) {
-                    $fMessages = $this->_attachToArray($fMessages, $arrayKeys[$key]);
-                    $messages = array_merge($messages, $fMessages);
+            $merge = $subForm->getMessages(null, true);
+            if (!empty($merge)) {
+                if (!$subForm->isArray()) {
+                    $merge = array($key => $merge);
                 } else {
-                    $messages[$key] = $fMessages;
+                    $merge = $this->_attachToArray($merge,
+                                                   $subForm->getElementsBelongTo());
                 }
+                $messages = array_merge_recursive($messages, $merge);
             }
         }

Comments

Patch applied to trunk and 1.10 release branch