Selaa lähdekoodia

merged branch yethee/choice_validator (PR #1713)

Commits
-------

0f328d2 [Validator] Fixed using the strict option in the choice validator.

Discussion
----------

[Validator] Fixed using the strict option in the choice validator.
Fabien Potencier 14 vuotta sitten
vanhempi
commit
e3a14c8ece

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

@@ -53,7 +53,7 @@ class ChoiceValidator extends ConstraintValidator
 
         if ($constraint->multiple) {
             foreach ($value as $_value) {
-                if (!in_array($_value, $choices, true)) {
+                if (!in_array($_value, $choices, $constraint->strict)) {
                     $this->setMessage($constraint->multipleMessage, array('{{ value }}' => $_value));
 
                     return false;

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

@@ -193,4 +193,37 @@ class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid('2', $constraint));
         $this->assertTrue($this->validator->isValid(2, $constraint));
     }
+
+    public function testStrictIsTrue()
+    {
+        $constraint = new Choice(array(
+            'choices' => array(1, 2),
+            'strict' => true,
+        ));
+
+        $this->assertTrue($this->validator->isValid(2, $constraint));
+        $this->assertFalse($this->validator->isValid('2', $constraint));
+    }
+
+    public function testStrictIsFalseWhenMultipleChoices()
+    {
+        $constraint = new Choice(array(
+            'choices' => array(1, 2, 3),
+            'multiple' => true,
+            'strict' => false
+        ));
+
+        $this->assertTrue($this->validator->isValid(array('2', 3), $constraint));
+    }
+
+    public function testStrictIsTrueWhenMultipleChoices()
+    {
+        $constraint = new Choice(array(
+            'choices' => array(1, 2, 3),
+            'multiple' => true,
+            'strict' => true
+        ));
+
+        $this->assertFalse($this->validator->isValid(array(2, '3'), $constraint));
+    }
 }