Procházet zdrojové kódy

[Validator] Allow DateTime objects as valid Times

Albert Jessurum před 14 roky
rodič
revize
ca52a04f5e

+ 4 - 0
src/Symfony/Component/Validator/Constraints/TimeValidator.php

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

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

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

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

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