From fe7f51ff8a1f53dcf76d0f492a0327c80b85f56e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysiek=20=C5=81abu=C5=9B?= Date: Tue, 22 Feb 2011 22:19:15 +0100 Subject: [PATCH] [Validator] Allow DateTime objects as valid Dates PHP doesn't provide any built-in class for date representation so we're often using DateTime object with time set to '00:00:00'. --- src/Symfony/Component/Validator/Constraints/DateValidator.php | 4 ++++ .../Component/Validator/Constraints/DateValidatorTest.php | 1 + 2 files changed, 5 insertions(+) diff --git a/src/Symfony/Component/Validator/Constraints/DateValidator.php b/src/Symfony/Component/Validator/Constraints/DateValidator.php index 0c6bf5fb0f50f..3013cd4a6a36a 100644 --- a/src/Symfony/Component/Validator/Constraints/DateValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateValidator.php @@ -25,6 +25,10 @@ public function isValid($value, Constraint $constraint) return true; } + if ($value instanceof \DateTime) { + return true; + } + if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString()'))) { throw new UnexpectedTypeException($value, 'string'); } diff --git a/tests/Symfony/Tests/Component/Validator/Constraints/DateValidatorTest.php b/tests/Symfony/Tests/Component/Validator/Constraints/DateValidatorTest.php index fd42c5906b601..b0325b4e550d5 100644 --- a/tests/Symfony/Tests/Component/Validator/Constraints/DateValidatorTest.php +++ b/tests/Symfony/Tests/Component/Validator/Constraints/DateValidatorTest.php @@ -54,6 +54,7 @@ public function getValidDates() array('2010-01-01'), array('1955-12-12'), array('2030-05-31'), + array(new \DateTime('2010-01-01')), ); }