Explorar el Código

fixes ZF-9289 setting allowEmpty to true in Zend_Filter_Input in general should not ignore specific NotEmpty rules

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23861 44c647ce-9c0f-0410-b52a-842ac1e357ba
mcleod@spaceweb.nl hace 14 años
padre
commit
dae98c4c06
Se han modificado 2 ficheros con 133 adiciones y 1 borrados
  1. 27 1
      library/Zend/Filter/Input.php
  2. 106 0
      tests/Zend/Filter/InputTest.php

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

@@ -839,7 +839,33 @@ class Zend_Filter_Input
                 $validatorRule[self::PRESENCE] = $this->_defaults[self::PRESENCE];
             }
             if (!isset($validatorRule[self::ALLOW_EMPTY])) {
-                $validatorRule[self::ALLOW_EMPTY] = $this->_defaults[self::ALLOW_EMPTY];
+                $foundNotEmptyValidator = false;
+                
+                foreach ($validatorRule as $rule) {
+                    if ($rule === 'NotEmpty') {
+                        $foundNotEmptyValidator = true;
+                        // field may not be empty, we are ready
+                        break 1;
+                    }
+                    
+                    // we must check if it is an object before using instanceof
+                    if (!is_object($rule)) {
+                        // it cannot be a NotEmpty validator, skip this one
+                        continue;
+                    }
+                    
+                    if($rule instanceof Zend_Validate_NotEmpty) {
+                        $foundNotEmptyValidator = true;
+                        // field may not be empty, we are ready
+                        break 1;
+                    }
+                }
+                
+                if (!$foundNotEmptyValidator) {
+                    $validatorRule[self::ALLOW_EMPTY] = $this->_defaults[self::ALLOW_EMPTY];
+                } else {
+                    $validatorRule[self::ALLOW_EMPTY] = false;
+                }
             }
 
             if (!isset($validatorRule[self::MESSAGES])) {

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

@@ -41,6 +41,112 @@ require_once 'Zend/Loader.php';
  */
 class Zend_Filter_InputTest extends PHPUnit_Framework_TestCase
 {
+    /**
+     * 
+     * If setAllowEmpty(true) is called, all fields are optional, but fields with
+     * a NotEmpty validator attached to them, should contain a non empty value.
+     * 
+     * @group ZF-9289
+     */
+    function testAllowEmptyTrueRespectsNotEmtpyValidators()
+    {
+        require_once 'Zend/Validate/NotEmpty.php';
+        require_once 'Zend/Validate/Digits.php';
+        
+        $data = array(
+            'field1' => 'foo',
+            '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()
+            )
+        );
+        
+        $options = array(Zend_Filter_Input::ALLOW_EMPTY => true);
+        $input = new Zend_Filter_Input( null, $validators, $data, $options );
+        $this->assertFalse($input->isValid(), 'Ouch, the NotEmpty validators are ignored!');
+        
+        $validators = array(
+            'field1' => array(
+                'Digits',
+                array('NotEmpty', 'integer'), 
+                Zend_Filter_Input::MESSAGES => array(
+                    1 => 
+                    array(
+                        Zend_Validate_NotEmpty::IS_EMPTY => '\'field1\' is required'
+                    )
+                ),
+            ),
+
+        );
+        
+        $data = array(
+            'field1' => 0,
+            'field2' => ''
+        );
+        $options = array(Zend_Filter_Input::ALLOW_EMPTY => true);
+        $input = new Zend_Filter_Input( null, $validators, $data, $options );
+        $this->assertFalse($input->isValid(), 'Ouch, if the NotEmpty validator is not the first rule, the NotEmpty validators are ignored !');
+        
+        // and now with a string 'NotEmpty' instead of an instance:
+        
+        $validators = array(
+            'field1' => array(
+                'NotEmpty',
+                Zend_Filter_Input::MESSAGES => array(
+                    0 => 
+                    array(
+                        Zend_Validate_NotEmpty::IS_EMPTY => '\'field1\' is required'
+                    )
+                ),
+            ),
+
+        );
+        
+        $data = array(
+            'field1' => '',
+            'field2' => ''
+        );
+        
+        $options = array(Zend_Filter_Input::ALLOW_EMPTY => true);
+        $input = new Zend_Filter_Input( null, $validators, $data, $options );
+        $this->assertFalse($input->isValid(), 'If the NotEmpty validator is a string, the NotEmpty validator is ignored !');
+        
+        // and now with an array
+        
+        $validators = array(
+            'field1' => array(
+                array('NotEmpty', 'integer'),
+                Zend_Filter_Input::MESSAGES => array(
+                    0 => 
+                    array(
+                        Zend_Validate_NotEmpty::IS_EMPTY => '\'field1\' is required'
+                    )
+                ),
+            ),
+
+        );
+        
+        $data = array(
+            'field1' => 0,
+            'field2' => ''
+        );
+        
+        $options = array(Zend_Filter_Input::ALLOW_EMPTY => true);
+        $input = new Zend_Filter_Input( null, $validators, $data, $options );
+        $this->assertFalse($input->isValid(), 'If the NotEmpty validator is an array, the NotEmpty validator is ignored !');
+    } 
 
      /**
       * @group ZF-8446