[Validator] Allow DateTime objects as valid Times

This commit is contained in:
Albert Jessurum 2011-06-09 15:47:51 +02:00
parent f3cafcb355
commit ca52a04f5e
3 changed files with 14 additions and 0 deletions

View File

@ -25,6 +25,10 @@ class TimeValidator extends ConstraintValidator
return true;
}
if ($value instanceof \DateTime) {
return true;
}
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}

View File

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

View File

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