Selaa lähdekoodia

fixes ZF-11267
If we pass in a validator instance that has a preset custom message, this
message is now used.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23862 44c647ce-9c0f-0410-b52a-842ac1e357ba

mcleod@spaceweb.nl 14 vuotta sitten
vanhempi
commit
f6cd64814c
2 muutettua tiedostoa jossa 68 lisäystä ja 6 poistoa
  1. 30 6
      library/Zend/Filter/Input.php
  2. 38 0
      tests/Zend/Filter/InputTest.php

+ 30 - 6
library/Zend/Filter/Input.php

@@ -907,7 +907,7 @@ 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
+                            /** 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
                             */ 
@@ -1018,9 +1018,12 @@ class Zend_Filter_Input
                 $messages         = array();
 
                 foreach ($data as $fieldKey => $field) {
-                    $notEmptyValidator = $this->_getValidator('NotEmpty');
-                    $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldKey));
-
+                    // if there is no Zend_Validate_NotEmpty instance in the rules, we will use the default
+                    if (!($notEmptyValidator = $this->_getNotEmptyValidatorInstance($validatorRule))) {
+                        $notEmptyValidator = $this->_getValidator('NotEmpty');
+                        $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldKey));
+                    }            
+                            
                     if (!$notEmptyValidator->isValid($field)) {
                         foreach ($notEmptyValidator->getMessages() as $messageKey => $message) {
                             if (!isset($messages[$messageKey])) {
@@ -1057,8 +1060,12 @@ class Zend_Filter_Input
                 $field = array($field);
             }
 
-            $notEmptyValidator = $this->_getValidator('NotEmpty');
-            $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldName));
+            // if there is no Zend_Validate_NotEmpty instance in the rules, we will use the default
+            if (!($notEmptyValidator = $this->_getNotEmptyValidatorInstance($validatorRule))) {
+                $notEmptyValidator = $this->_getValidator('NotEmpty');
+                $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldName));
+            }
+            
             if ($validatorRule[self::ALLOW_EMPTY]) {
                 $validatorChain = $validatorRule[self::VALIDATOR_CHAIN];
             } else {
@@ -1116,6 +1123,23 @@ class Zend_Filter_Input
             }
         }
     }
+    
+    /**
+     * Check a validatorRule for the presence of a NotEmpty validator instance.
+     * The purpose is to preserve things like a custom message, that may have been 
+     * set on the validator outside Zend_Filter_Input.
+     * @param array $validatorRule
+     * @return mixed false if none is found, Zend_Validate_NotEmpty instance if found
+     */
+    protected function _getNotEmptyValidatorInstance($validatorRule) {
+        foreach ($validatorRule as $rule => $value) {
+            if (is_object($value) and $value instanceof Zend_Validate_NotEmpty) {
+                return $value;
+            }
+        }
+        
+        return false;
+    }
 
     /**
      * @param mixed $classBaseName

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

@@ -42,6 +42,44 @@ require_once 'Zend/Loader.php';
 class Zend_Filter_InputTest extends PHPUnit_Framework_TestCase
 {
     /**
+     * @group ZF-11267
+     * If we pass in a validator instance that has a preset custom message, this
+     * message should be used.
+     */
+    function testIfCustomMessagesOnValidatorInstancesCanBeUsed()
+    {
+        // test with a Digits validator
+        require_once 'Zend/Validate/Digits.php';
+        require_once 'Zend/Validate/NotEmpty.php';
+        $data = array('field1' => 'invalid data');
+        $customMessage = 'Hey, that\'s not a Digit!!!';
+        $validator = new Zend_Validate_Digits();
+        $validator->setMessage($customMessage, 'notDigits');
+        $this->assertFalse($validator->isValid('foo'), 'standalone validator thinks \'foo\' is a valid digit');
+        $messages = $validator->getMessages();
+        $this->assertSame($messages['notDigits'], $customMessage, 'stanalone validator does not have custom message');
+        $validators = array('field1' => $validator);
+        $input = new Zend_Filter_Input(null, $validators, $data);
+        $this->assertFalse($input->isValid(), 'invalid input is valid');
+        $messages = $input->getMessages();
+        $this->assertSame($messages['field1']['notDigits'], $customMessage, 'The custom message is not used');
+        
+        // test with a NotEmpty validator
+        $data = array('field1' => '');
+        $customMessage = 'You should really supply a value...';
+        $validator = new Zend_Validate_NotEmpty();
+        $validator->setMessage($customMessage, 'isEmpty');
+        $this->assertFalse($validator->isValid(''), 'standalone validator thinks \'\' is not empty');
+        $messages = $validator->getMessages();
+        $this->assertSame($messages['isEmpty'], $customMessage, 'stanalone NotEmpty validator does not have custom message');
+        $validators = array('field1' => $validator);
+        $input = new Zend_Filter_Input(null, $validators, $data);
+        $this->assertFalse($input->isValid(), 'invalid input is valid');
+        $messages = $input->getMessages();
+        $this->assertSame($messages['field1']['isEmpty'], $customMessage, 'For the NotEmpty validator the custom message is not used');
+    }
+    
+    /**
      * 
      * If setAllowEmpty(true) is called, all fields are optional, but fields with
      * a NotEmpty validator attached to them, should contain a non empty value.