From 136c296aeeead1d0de0ab3b0ceea2d68674f815a Mon Sep 17 00:00:00 2001 From: Thierry Thuon Date: Mon, 6 Mar 2017 00:14:39 +0100 Subject: [PATCH] update datetime validator to validate format only --- UPGRADE-3.3.md | 5 ++++ UPGRADE-4.0.md | 2 ++ src/Symfony/Component/Validator/CHANGELOG.md | 2 ++ .../Component/Validator/Constraints/Date.php | 4 +++ .../Validator/Constraints/DateTime.php | 16 ++++++++++++ .../Constraints/DateTimeValidator.php | 24 +++-------------- .../Validator/Constraints/DateValidator.php | 4 +++ .../Component/Validator/Constraints/Time.php | 4 +++ .../Validator/Constraints/TimeValidator.php | 4 +++ .../Constraints/DateTimeValidatorTest.php | 26 +++++++++---------- .../Tests/Constraints/DateValidatorTest.php | 3 +++ .../Tests/Constraints/TimeValidatorTest.php | 3 +++ 12 files changed, 63 insertions(+), 34 deletions(-) diff --git a/UPGRADE-3.3.md b/UPGRADE-3.3.md index 66ebe0f742c5f..5d10116c9568a 100644 --- a/UPGRADE-3.3.md +++ b/UPGRADE-3.3.md @@ -294,6 +294,11 @@ TwigBundle * The `ContainerAwareRuntimeLoader` class has been deprecated and will be removed in 4.0. Use the Twig `Twig_ContainerRuntimeLoader` class instead. + +Validator +--------- + + * `DateValidator` and `TimeValidator` have been deprecated. Use `DateTimeValidator` instead. Workflow -------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index e2246364f6390..69a5b66bdbe47 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -494,6 +494,8 @@ Validator changed to `true` as of 4.0. If you need the previous behaviour ensure to set the option to `false`. + * `DateValidator` and `TimeValidator` have been removed. Use `DateTimeValidator` instead. + Workflow -------- diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 7099496cfa1d2..a178f0c0ecf0c 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -6,6 +6,8 @@ CHANGELOG * added `AddValidatorInitializersPass` * added `AddConstraintValidatorsPass` + * deprecated `DateValidator` in favor of `DateTimeValidator` + * deprecated `TimeValidator` in favor of `DateTimeValidator` 3.2.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/Date.php b/src/Symfony/Component/Validator/Constraints/Date.php index 256341312246c..870231a54017e 100644 --- a/src/Symfony/Component/Validator/Constraints/Date.php +++ b/src/Symfony/Component/Validator/Constraints/Date.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Validator\Constraints; +@trigger_error('The '.__NAMESPACE__.'\Date class is deprecated since version 3.3 and will be removed in 4.0. Use '.__NAMESPACE__.'\DateTime instead.', E_USER_DEPRECATED); + use Symfony\Component\Validator\Constraint; /** @@ -18,6 +20,8 @@ * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) * * @author Bernhard Schussek + * + * @deprecated Since version 3.3, to be removed in 4.0. Use Symfony\Component\Validator\Constraints\DateTimeValidator instead. */ class Date extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/DateTime.php b/src/Symfony/Component/Validator/Constraints/DateTime.php index c65f185ae428c..115ea77f879ff 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTime.php +++ b/src/Symfony/Component/Validator/Constraints/DateTime.php @@ -22,7 +22,15 @@ class DateTime extends Constraint { const INVALID_FORMAT_ERROR = '1a9da513-2640-4f84-9b6a-4d99dcddc628'; + + /** + * @deprecated since version 3.3, to be removed in 4.0. + */ const INVALID_DATE_ERROR = 'd52afa47-620d-4d99-9f08-f4d85b36e33c'; + + /** + * @deprecated since version 3.3, to be removed in 4.0. + */ const INVALID_TIME_ERROR = '5e797c9d-74f7-4098-baa3-94390c447b27'; protected static $errorNames = array( @@ -33,4 +41,12 @@ class DateTime extends Constraint public $format = 'Y-m-d H:i:s'; public $message = 'This value is not a valid datetime.'; + + /** + * {@inheritdoc} + */ + public function getDefaultOption() + { + return 'format'; + } } diff --git a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php index af956ee06b583..828528d49a9c5 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php @@ -12,13 +12,14 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * @author Bernhard Schussek * @author Diego Saint Esteben */ -class DateTimeValidator extends DateValidator +class DateTimeValidator extends ConstraintValidator { /** * @deprecated since version 3.1, to be removed in 4.0. @@ -48,7 +49,7 @@ public function validate($value, Constraint $constraint) $errors = \DateTime::getLastErrors(); - if (0 < $errors['error_count']) { + if (0 < $errors['error_count'] || 0 < $errors['warning_count']) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(DateTime::INVALID_FORMAT_ERROR) @@ -56,24 +57,5 @@ public function validate($value, Constraint $constraint) return; } - - foreach ($errors['warnings'] as $warning) { - if ('The parsed date was invalid' === $warning) { - $this->context->buildViolation($constraint->message) - ->setParameter('{{ value }}', $this->formatValue($value)) - ->setCode(DateTime::INVALID_DATE_ERROR) - ->addViolation(); - } elseif ('The parsed time was invalid' === $warning) { - $this->context->buildViolation($constraint->message) - ->setParameter('{{ value }}', $this->formatValue($value)) - ->setCode(DateTime::INVALID_TIME_ERROR) - ->addViolation(); - } else { - $this->context->buildViolation($constraint->message) - ->setParameter('{{ value }}', $this->formatValue($value)) - ->setCode(DateTime::INVALID_FORMAT_ERROR) - ->addViolation(); - } - } } } diff --git a/src/Symfony/Component/Validator/Constraints/DateValidator.php b/src/Symfony/Component/Validator/Constraints/DateValidator.php index ed836de9aced2..ab1e28769435b 100644 --- a/src/Symfony/Component/Validator/Constraints/DateValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateValidator.php @@ -11,12 +11,16 @@ namespace Symfony\Component\Validator\Constraints; +@trigger_error('The '.__NAMESPACE__.'\DateValidator class is deprecated since version 3.3 and will be removed in 4.0. Use '.__NAMESPACE__.'\DateTimeValidator instead.', E_USER_DEPRECATED); + use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * @author Bernhard Schussek + * + * @deprecated Since version 3.3, to be removed in 4.0. Use Symfony\Component\Validator\Constraints\DateTimeValidator instead. */ class DateValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Constraints/Time.php b/src/Symfony/Component/Validator/Constraints/Time.php index 6bd8dbda2e2aa..2a53502627f33 100644 --- a/src/Symfony/Component/Validator/Constraints/Time.php +++ b/src/Symfony/Component/Validator/Constraints/Time.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Validator\Constraints; +@trigger_error('The '.__NAMESPACE__.'\Time class is deprecated since version 3.3 and will be removed in 4.0. Use '.__NAMESPACE__.'\DateTime instead.', E_USER_DEPRECATED); + use Symfony\Component\Validator\Constraint; /** @@ -18,6 +20,8 @@ * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) * * @author Bernhard Schussek + * + * @deprecated Since version 3.3, to be removed in 4.0. Use Symfony\Component\Validator\Constraints\DateTimeValidator instead. */ class Time extends Constraint { diff --git a/src/Symfony/Component/Validator/Constraints/TimeValidator.php b/src/Symfony/Component/Validator/Constraints/TimeValidator.php index 3f0234b8b0896..a5a9f54b6bd68 100644 --- a/src/Symfony/Component/Validator/Constraints/TimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/TimeValidator.php @@ -11,12 +11,16 @@ namespace Symfony\Component\Validator\Constraints; +@trigger_error('The '.__NAMESPACE__.'\TimeValidator class is deprecated since version 3.3 and will be removed in 4.0. Use '.__NAMESPACE__.'\DateTimeValidator instead.', E_USER_DEPRECATED); + use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * @author Bernhard Schussek + * + * @deprecated Since version 3.3, to be removed in 4.0. Use Symfony\Component\Validator\Constraints\DateTimeValidator instead. */ class TimeValidator extends ConstraintValidator { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php index fe553100339f8..5421667cf9e45 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php @@ -24,28 +24,28 @@ protected function createValidator() public function testNullIsValid() { - $this->validator->validate(null, new DateTime()); + $this->validator->validate(null, new DateTime('Y-m-d H:i:s')); $this->assertNoViolation(); } public function testEmptyStringIsValid() { - $this->validator->validate('', new DateTime()); + $this->validator->validate('', new DateTime('Y-m-d H:i:s')); $this->assertNoViolation(); } public function testDateTimeClassIsValid() { - $this->validator->validate(new \DateTime(), new DateTime()); + $this->validator->validate(new \DateTime(), new DateTime('Y-m-d H:i:s')); $this->assertNoViolation(); } public function testDateTimeImmutableClassIsValid() { - $this->validator->validate(new \DateTimeImmutable(), new DateTime()); + $this->validator->validate(new \DateTimeImmutable(), new DateTime('Y-m-d H:i:s')); $this->assertNoViolation(); } @@ -55,16 +55,16 @@ public function testDateTimeImmutableClassIsValid() */ public function testExpectsStringCompatibleType() { - $this->validator->validate(new \stdClass(), new DateTime()); + $this->validator->validate(new \stdClass(), new DateTime('Y-m-d H:i:s')); } public function testDateTimeWithDefaultFormat() { - $this->validator->validate('1995-05-10 19:33:00', new DateTime()); + $this->validator->validate('1995-05-10 19:33:00', new DateTime('Y-m-d H:i:s')); $this->assertNoViolation(); - $this->validator->validate('1995-03-24', new DateTime()); + $this->validator->validate('1995-03-24', new DateTime('Y-m-d H:i:s')); $this->buildViolation('This value is not a valid datetime.') ->setParameter('{{ value }}', '"1995-03-24"') @@ -122,12 +122,12 @@ public function getInvalidDateTimes() array('H:i', '00:00:00', DateTime::INVALID_FORMAT_ERROR), array('Y-m-d', '2010-01-01 00:00', DateTime::INVALID_FORMAT_ERROR), array('Y-m-d e', '2010-01-01 TCU', DateTime::INVALID_FORMAT_ERROR), - array('Y-m-d H:i:s', '2010-13-01 00:00:00', DateTime::INVALID_DATE_ERROR), - array('Y-m-d H:i:s', '2010-04-32 00:00:00', DateTime::INVALID_DATE_ERROR), - array('Y-m-d H:i:s', '2010-02-29 00:00:00', DateTime::INVALID_DATE_ERROR), - array('Y-m-d H:i:s', '2010-01-01 24:00:00', DateTime::INVALID_TIME_ERROR), - array('Y-m-d H:i:s', '2010-01-01 00:60:00', DateTime::INVALID_TIME_ERROR), - array('Y-m-d H:i:s', '2010-01-01 00:00:60', DateTime::INVALID_TIME_ERROR), + array('Y-m-d H:i:s', '2010-13-01 00:00:00', DateTime::INVALID_FORMAT_ERROR), + array('Y-m-d H:i:s', '2010-04-32 00:00:00', DateTime::INVALID_FORMAT_ERROR), + array('Y-m-d H:i:s', '2010-02-29 00:00:00', DateTime::INVALID_FORMAT_ERROR), + array('Y-m-d H:i:s', '2010-01-01 24:00:00', DateTime::INVALID_FORMAT_ERROR), + array('Y-m-d H:i:s', '2010-01-01 00:60:00', DateTime::INVALID_FORMAT_ERROR), + array('Y-m-d H:i:s', '2010-01-01 00:00:60', DateTime::INVALID_FORMAT_ERROR), ); } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php index 3b2b189a55215..d76c1921668de 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php @@ -15,6 +15,9 @@ use Symfony\Component\Validator\Constraints\DateValidator; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; +/** + * @group legacy + */ class DateValidatorTest extends ConstraintValidatorTestCase { protected function createValidator() diff --git a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php index a22251d0ee3c5..f9ca08a790e47 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php @@ -15,6 +15,9 @@ use Symfony\Component\Validator\Constraints\TimeValidator; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; +/** + * @group legacy + */ class TimeValidatorTest extends ConstraintValidatorTestCase { protected function createValidator()