Bladeren bron

[Validator] Fixed string-based constraint validators to accept empty values

Bernhard Schussek 14 jaren geleden
bovenliggende
commit
1b2ca259f1

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

@@ -21,7 +21,7 @@ class DateTimeValidator extends ConstraintValidator
 
     public function isValid($value, Constraint $constraint)
     {
-        if ($value === null) {
+        if ($value === null || $value === '') {
             return true;
         }
 

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

@@ -21,7 +21,7 @@ class DateValidator extends ConstraintValidator
 
     public function isValid($value, Constraint $constraint)
     {
-        if ($value === null) {
+        if ($value === null || $value === '') {
             return true;
         }
 

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

@@ -21,7 +21,7 @@ class EmailValidator extends ConstraintValidator
 
     public function isValid($value, Constraint $constraint)
     {
-        if ($value === null) {
+        if ($value === null || $value === '') {
             return true;
         }
 

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

@@ -21,7 +21,7 @@ class FileValidator extends ConstraintValidator
 {
     public function isValid($value, Constraint $constraint)
     {
-        if ($value === null) {
+        if ($value === null || $value === '') {
             return true;
         }
 

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

@@ -19,7 +19,7 @@ class MaxLengthValidator extends ConstraintValidator
 {
     public function isValid($value, Constraint $constraint)
     {
-        if ($value === null) {
+        if ($value === null || $value === '') {
             return true;
         }
 

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

@@ -19,7 +19,7 @@ class MinLengthValidator extends ConstraintValidator
 {
     public function isValid($value, Constraint $constraint)
     {
-        if ($value === null) {
+        if ($value === null || $value === '') {
             return true;
         }
 

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

@@ -19,7 +19,7 @@ class RegexValidator extends ConstraintValidator
 {
     public function isValid($value, Constraint $constraint)
     {
-        if ($value === null) {
+        if ($value === null || $value === '') {
             return true;
         }
 

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

@@ -21,7 +21,7 @@ class TimeValidator extends ConstraintValidator
 
     public function isValid($value, Constraint $constraint)
     {
-        if ($value === null) {
+        if ($value === null || $value === '') {
             return true;
         }
 

+ 5 - 0
tests/Symfony/Tests/Component/Validator/Constraints/DateTimeValidatorTest.php

@@ -19,6 +19,11 @@ class DateTimeValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new DateTime()));
     }
 
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new DateTime()));
+    }
+
     public function testExpectsStringCompatibleType()
     {
         $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

+ 5 - 0
tests/Symfony/Tests/Component/Validator/Constraints/DateValidatorTest.php

@@ -19,6 +19,11 @@ class DateValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new Date()));
     }
 
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new Date()));
+    }
+
     public function testExpectsStringCompatibleType()
     {
         $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

+ 5 - 0
tests/Symfony/Tests/Component/Validator/Constraints/EmailValidatorTest.php

@@ -19,6 +19,11 @@ class EmailValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new Email()));
     }
 
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new Email()));
+    }
+
     public function testExpectsStringCompatibleType()
     {
         $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

+ 5 - 0
tests/Symfony/Tests/Component/Validator/Constraints/FileValidatorTest.php

@@ -28,6 +28,11 @@ class FileValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new File()));
     }
 
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new File()));
+    }
+
     public function testExpectsStringCompatibleTypeOrFile()
     {
         $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

+ 5 - 0
tests/Symfony/Tests/Component/Validator/Constraints/MaxLengthValidatorTest.php

@@ -19,6 +19,11 @@ class MaxLengthValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new MaxLength(array('limit' => 5))));
     }
 
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new MaxLength(array('limit' => 5))));
+    }
+
     public function testExpectsStringCompatibleType()
     {
         $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

+ 5 - 0
tests/Symfony/Tests/Component/Validator/Constraints/MinLengthValidatorTest.php

@@ -19,6 +19,11 @@ class MinLengthValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new MinLength(array('limit' => 6))));
     }
 
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new MinLength(array('limit' => 6))));
+    }
+
     public function testExpectsStringCompatibleType()
     {
         $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

+ 5 - 0
tests/Symfony/Tests/Component/Validator/Constraints/RegexValidatorTest.php

@@ -19,6 +19,11 @@ class RegexValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new Regex(array('pattern' => '/^[0-9]+$/'))));
     }
 
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new Regex(array('pattern' => '/^[0-9]+$/'))));
+    }
+
     public function testExpectsStringCompatibleType()
     {
         $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');

+ 5 - 0
tests/Symfony/Tests/Component/Validator/Constraints/TimeValidatorTest.php

@@ -19,6 +19,11 @@ class TimeValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new Time()));
     }
 
+    public function testEmptyStringIsValid()
+    {
+        $this->assertTrue($this->validator->isValid('', new Time()));
+    }
+
     public function testExpectsStringCompatibleType()
     {
         $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');