Prechádzať zdrojové kódy

[Validator] Added strict option to ChoiceConstraint.

marc.weistroff 14 rokov pred
rodič
commit
df57e0fe9a

+ 1 - 0
src/Symfony/Component/Validator/Constraints/Choice.php

@@ -19,6 +19,7 @@ class Choice extends Constraint
     public $choices;
     public $choices;
     public $callback;
     public $callback;
     public $multiple = false;
     public $multiple = false;
+    public $strict = false;
     public $min = null;
     public $min = null;
     public $max = null;
     public $max = null;
     public $message = 'The value you selected is not a valid choice';
     public $message = 'The value you selected is not a valid choice';

+ 1 - 1
src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

@@ -73,7 +73,7 @@ class ChoiceValidator extends ConstraintValidator
 
 
                 return false;
                 return false;
             }
             }
-        } elseif (!in_array($value, $choices, true)) {
+        } elseif (!in_array($value, $choices, $constraint->strict)) {
             $this->setMessage($constraint->message, array('{{ value }}' => $value));
             $this->setMessage($constraint->message, array('{{ value }}' => $value));
 
 
             return false;
             return false;

+ 11 - 0
tests/Symfony/Tests/Component/Validator/Constraints/ChoiceValidatorTest.php

@@ -182,4 +182,15 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
             '{{ limit }}' => 2,
             '{{ limit }}' => 2,
         ));
         ));
     }
     }
+
+    public function testStrictIsFalse()
+    {
+        $constraint = new Choice(array(
+            'choices' => array(1, 2),
+            'strict' => false,
+        ));
+
+        $this->assertTrue($this->validator->isValid('2', $constraint));
+        $this->assertTrue($this->validator->isValid(2, $constraint));
+    }
 }
 }