Bläddra i källkod

Per the [documentation][1], the `NotBlank` constraint should be using the `empty` language construct, otherwise it will not trigger on, for example, a boolean false from an unchecked checkbox field.

[1]: http://symfony.com/doc/2.0/reference/constraints/NotBlank.html
Evan Kaufman 13 år sedan
förälder
incheckning
639513a67a

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

@@ -31,7 +31,7 @@ class NotBlankValidator extends ConstraintValidator
      */
     public function isValid($value, Constraint $constraint)
     {
-        if (null === $value || '' === $value) {
+        if (false === $value || (empty($value) && '0' != $value)) {
             $this->setMessage($constraint->message);
 
             return false;

+ 16 - 4
tests/Symfony/Tests/Component/Validator/Constraints/NotBlankValidatorTest.php

@@ -29,19 +29,20 @@ class NotBlankValidatorTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @dataProvider getInvalidValues
+     * @dataProvider getValidValues
      */
-    public function testInvalidValues($date)
+    public function testValidValues($date)
     {
         $this->assertTrue($this->validator->isValid($date, new NotBlank()));
     }
 
-    public function getInvalidValues()
+    public function getValidValues()
     {
         return array(
             array('foobar'),
             array(0),
-            array(false),
+            array(0.0),
+            array('0'),
             array(1234),
         );
     }
@@ -56,6 +57,16 @@ class NotBlankValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($this->validator->isValid('', new NotBlank()));
     }
 
+    public function testFalseIsInvalid()
+    {
+        $this->assertFalse($this->validator->isValid(false, new NotBlank()));
+    }
+
+    public function testEmptyArrayIsInvalid()
+    {
+        $this->assertFalse($this->validator->isValid(array(), new NotBlank()));
+    }
+
     public function testSetMessage()
     {
         $constraint = new NotBlank(array(
@@ -67,3 +78,4 @@ class NotBlankValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($this->validator->getMessageParameters(), array());
     }
 }
+