Browse Source

fix and unit test for nesting error messages issue ZF-8446

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23858 44c647ce-9c0f-0410-b52a-842ac1e357ba
mcleod@spaceweb.nl 14 years ago
parent
commit
1ba27c5a66
2 changed files with 67 additions and 1 deletions
  1. 7 1
      library/Zend/Filter/Input.php
  2. 60 0
      tests/Zend/Filter/InputTest.php

+ 7 - 1
library/Zend/Filter/Input.php

@@ -885,7 +885,13 @@ class Zend_Filter_Input
                             * validator, but it goes wrong if one of them is not AND is required!!!
                             * that is why we restore the default value at the end of this loop
                             */ 
-                            $this->_defaults[self::NOT_EMPTY_MESSAGE] = $value;
+                            if (is_array($value)) {
+                                $temp = $value; // keep the original value
+                                $this->_defaults[self::NOT_EMPTY_MESSAGE] = array_pop($temp);
+                                unset($temp);
+                            } else {
+                                $this->_defaults[self::NOT_EMPTY_MESSAGE] = $value;
+                            }
                         }
                     }
 

+ 60 - 0
tests/Zend/Filter/InputTest.php

@@ -41,6 +41,66 @@ require_once 'Zend/Loader.php';
  */
 class Zend_Filter_InputTest extends PHPUnit_Framework_TestCase
 {
+
+     /**
+      * @group ZF-8446
+      * The issue reports about nested error messages. This is to assure these do not occur.
+      * 
+      * Example:
+      * Expected Result
+      *      array(2) {
+      *        ["field1"] => array(1) {
+      *          ["isEmpty"] => string(20) "'field1' is required"
+      *        }
+      *        ["field2"] => array(1) {
+      *          ["isEmpty"] => string(36) "Value is required and can't be empty"
+      *        }
+      *      }
+      *  Actual Result
+      *      array(2) {
+      *        ["field1"] => array(1) {
+      *          ["isEmpty"] => array(1) {
+      *            ["isEmpty"] => string(20) "'field1' is required"
+      *          }
+      *        }
+      *        ["field2"] => array(1) {
+      *          ["isEmpty"] => array(1) {
+      *            ["isEmpty"] => string(20) "'field1' is required"
+      *          }
+      *        }
+      *      }
+     */
+    public function testNoNestedMessageArrays()
+    {
+        require_once 'Zend/Validate/NotEmpty.php';
+        $data = array(
+            'field1' => '',
+            'field2' => ''
+        );
+        
+        $validators = array(
+            'field1' => array(
+                new Zend_Validate_NotEmpty(),
+                Zend_Filter_Input::MESSAGES => array(
+                    array(
+                        Zend_Validate_NotEmpty::IS_EMPTY => '\'field1\' is required'
+                    )
+                )
+            ),
+        
+            'field2' => array(
+                new Zend_Validate_NotEmpty()
+            )
+        );
+        
+        $input = new Zend_Filter_Input( null, $validators, $data );
+        
+        $this->assertFalse($input->isValid());
+        $messages = $input->getMessages();
+        $this->assertFalse(is_array($messages['field1']['isEmpty']), 'oh oh, we  may have got nested messages');
+        $this->assertTrue(isset($messages['field1']['isEmpty']), 'oh no, we not even got the normally expected messages');
+    }
+    
     /**
      * @group ZF-11142, ZF-8446, ZF-9289
      */