Forráskód Böngészése

ZF-9289: better NotEmpty validator detection

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24471 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 14 éve
szülő
commit
dd81b4163f
2 módosított fájl, 112 hozzáadás és 1 törlés
  1. 12 1
      library/Zend/Filter/Input.php
  2. 100 0
      tests/Zend/Filter/InputTest.php

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

@@ -848,6 +848,17 @@ class Zend_Filter_Input
                         break 1;
                     }
                     
+                    if (is_array($rule)) {
+                        $keys      = array_keys($rule);
+                        $classKey  = array_shift($keys);
+                        $ruleClass = $rule[$classKey];
+                        if ($ruleClass === '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
@@ -1074,7 +1085,7 @@ class Zend_Filter_Input
                 $validatorChain->addValidator($validatorRule[self::VALIDATOR_CHAIN]);
             }
 
-            foreach ($field as $value) {
+            foreach ($field as $key => $value) {
                 if ($validatorRule[self::ALLOW_EMPTY]  &&  !$notEmptyValidator->isValid($value)) {
                     // Field is empty AND it's allowed. Do nothing.
                     continue;

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

@@ -2182,6 +2182,106 @@ class Zend_Filter_InputTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(array('rule1'), array_keys($missing));
         $this->assertEquals(array("Still missing"), $missing['rule1']);
     }
+
+    /**
+     * 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 testAllowEmptyTrueRespectsNotEmptyValidators()
+    {
+        $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 !');
+    }
 }
 
 class MyZend_Filter_Date implements Zend_Filter_Interface