Parcourir la source

Fixes ZF-11142, ZF-8446, ZF-9289. These are about a custom message for a NotEmpty validator inheriting to all subsequent required fields or NotEmpty validators not having a custom message of their own. I commit this because the owners and original assignees have not replied to the patches or comments.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23822 44c647ce-9c0f-0410-b52a-842ac1e357ba
mcleod@spaceweb.nl il y a 15 ans
Parent
commit
049081b050
2 fichiers modifiés avec 42 ajouts et 0 suppressions
  1. 14 0
      library/Zend/Filter/Input.php
  2. 28 0
      tests/Zend/Filter/InputTest.php

+ 14 - 0
library/Zend/Filter/Input.php

@@ -801,6 +801,9 @@ class Zend_Filter_Input
             $this->_data = array();
             return;
         }
+        
+        // remember the default not empty message in case we want to temporarily change it        
+        $preserveDefaultNotEmptyMessage = $this->_defaults[self::NOT_EMPTY_MESSAGE];
 
         foreach ($this->_validatorRules as $ruleName => &$validatorRule) {
             /**
@@ -844,6 +847,8 @@ class Zend_Filter_Input
             } else if (!is_array($validatorRule[self::MESSAGES])) {
                 $validatorRule[self::MESSAGES] = array($validatorRule[self::MESSAGES]);
             } else if (array_intersect_key($validatorList, $validatorRule[self::MESSAGES])) {
+                // this seems pointless... it just re-adds what it already has...
+                // I can disable all this and not a single unit test fails...
                 // There are now corresponding numeric keys in the validation rule messages array
                 // Treat it as a named messages list for all rule validators
                 $unifiedMessages = $validatorRule[self::MESSAGES];
@@ -876,6 +881,10 @@ class Zend_Filter_Input
                         }
 
                         if ($validator instanceof Zend_Validate_NotEmpty) {
+                            /* we are changing the defaults here, this is alright if all subsequent validators are also a not empty
+                            * 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;
                         }
                     }
@@ -897,7 +906,12 @@ class Zend_Filter_Input
             } else {
                 $this->_validateRule($validatorRule);
             }
+            
+            // reset the default not empty message
+            $this->_defaults[self::NOT_EMPTY_MESSAGE] = $preserveDefaultNotEmptyMessage;
         }
+        
+
 
         /**
          * Unset fields in $_data that have been added to other arrays.

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

@@ -41,6 +41,34 @@ require_once 'Zend/Loader.php';
  */
 class Zend_Filter_InputTest extends PHPUnit_Framework_TestCase
 {
+    /**
+     * @group ZF-11142, ZF-8446, ZF-9289
+     */
+    public function testTwoValidatorsInChainShowCorrectError()
+    {
+        require_once 'Zend/Validate/NotEmpty.php';
+        require_once 'Zend/Validate/Float.php';
+        $validators = array(
+            'field1'  => array(
+                    'NotEmpty', 'Float',
+                    'presence'  => 'required',
+                    'messages'  => array(
+                        'Field1 is empty',
+                        array(Zend_Validate_Float::NOT_FLOAT => "Field1 must be a number.")
+                    )
+                ),
+            'field2'    => array(
+                    'presence' => 'required'
+                )
+        );
+        
+        $data = array('field1' => 0.0, 'field2' => '');
+        $input = new Zend_Filter_Input(null, $validators, $data);
+        $this->assertFalse($input->isValid());
+        $messages = $input->getMessages();
+        $this->assertSame($messages['field2']["isEmpty"], "You must give a non-empty value for field 'field2'");
+        $this->assertSame('Field1 is empty', $messages['field1'][Zend_Validate_NotEmpty::IS_EMPTY], 'custom message not shown');
+    }
 
     public function testFilterDeclareSingle()
     {