From 27f6e28f5b3858497019097572351e2db948fce6 Mon Sep 17 00:00:00 2001 From: Stefan Kleff Date: Thu, 4 Jun 2020 13:47:59 +0200 Subject: [PATCH] DateTime validator support for trailing data --- .../Component/Validator/Constraints/DateTimeValidator.php | 6 ++++++ .../Validator/Tests/Constraints/DateTimeValidatorTest.php | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php index bad043f20e..8d0350532d 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php @@ -57,6 +57,12 @@ class DateTimeValidator extends DateValidator return; } + if('+' === substr($constraint->format, -1)) { + $errors['warnings'] = array_filter($errors['warnings'], function($warning) { + return 'Trailing data' !== $warning; + }); + } + foreach ($errors['warnings'] as $warning) { if ('The parsed date was invalid' === $warning) { $this->context->buildViolation($constraint->message) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php index 55bfe45143..8ff515772d 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php @@ -128,4 +128,12 @@ class DateTimeValidatorTest extends ConstraintValidatorTestCase ['Y-m-d H:i:s', '2010-01-01 00:00:60', DateTime::INVALID_TIME_ERROR], ]; } + + public function testDateTimeWithTrailingData() + { + $this->validator->validate('1995-05-10 00:00:00', new DateTime([ + 'format' => 'Y-m-d+', + ])); + $this->assertNoViolation(); + } }