[Validator] Fixed string-based constraint validators to accept empty values
This commit is contained in:
parent
af291bb0f1
commit
1b2ca259f1
@ -21,7 +21,7 @@ class DateTimeValidator extends ConstraintValidator
|
||||
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($value === null || $value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ class DateValidator extends ConstraintValidator
|
||||
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($value === null || $value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ class EmailValidator extends ConstraintValidator
|
||||
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($value === null || $value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ class FileValidator extends ConstraintValidator
|
||||
{
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($value === null || $value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,7 @@ class MaxLengthValidator extends ConstraintValidator
|
||||
{
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($value === null || $value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,7 @@ class MinLengthValidator extends ConstraintValidator
|
||||
{
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($value === null || $value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,7 @@ class RegexValidator extends ConstraintValidator
|
||||
{
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($value === null || $value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ class TimeValidator extends ConstraintValidator
|
||||
|
||||
public function isValid($value, Constraint $constraint)
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($value === null || $value === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -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');
|
||||
|
@ -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');
|
||||
|
@ -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');
|
||||
|
@ -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');
|
||||
|
@ -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');
|
||||
|
@ -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');
|
||||
|
@ -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');
|
||||
|
@ -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');
|
||||
|
Reference in New Issue
Block a user