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

This commit is contained in:
Bernhard Schussek 2010-12-10 10:47:43 +01:00 committed by Fabien Potencier
parent af291bb0f1
commit 1b2ca259f1
16 changed files with 48 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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');

View File

@ -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');

View File

@ -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');

View File

@ -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');

View File

@ -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');

View File

@ -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');

View File

@ -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');

View File

@ -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');