diff --git a/UPGRADE-3.1.md b/UPGRADE-3.1.md index d334c75fd951..8c59795bd209 100644 --- a/UPGRADE-3.1.md +++ b/UPGRADE-3.1.md @@ -117,3 +117,9 @@ Yaml * The `!!php/object` tag to indicate dumped PHP objects has been deprecated and will be removed in Symfony 4.0. Use the `!php/object` tag instead. + +Validator +--------- + + * The `DateTimeValidator::PATTERN` constant is deprecated and will be removed in + Symfony 4.0. diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index d9e3b71fd65b..1f594037bddd 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -113,3 +113,8 @@ Yaml * The `!!php/object` tag to indicate dumped PHP objects was removed in favor of the `!php/object` tag. + +Validator +--------- + + * The `DateTimeValidator::PATTERN` constant was removed. diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 0ff667b77068..23249ae12390 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +3.1.0 +----- + + * deprecated `DateTimeValidator::PATTERN` constant + * added a `format` option to the `DateTime` constraint + 2.8.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/DateTime.php b/src/Symfony/Component/Validator/Constraints/DateTime.php index 35e29345d097..c65f185ae428 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTime.php +++ b/src/Symfony/Component/Validator/Constraints/DateTime.php @@ -31,5 +31,6 @@ class DateTime extends Constraint self::INVALID_TIME_ERROR => 'INVALID_TIME_ERROR', ); + public $format = 'Y-m-d H:i:s'; public $message = 'This value is not a valid datetime.'; } diff --git a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php index 8d82bd8ca9a4..681b7bdbee01 100644 --- a/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php +++ b/src/Symfony/Component/Validator/Constraints/DateTimeValidator.php @@ -16,9 +16,13 @@ /** * @author Bernhard Schussek + * @author Diego Saint Esteben */ class DateTimeValidator extends DateValidator { + /** + * @deprecated since version 3.1, to be removed in 4.0. + */ const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/'; /** @@ -40,7 +44,11 @@ public function validate($value, Constraint $constraint) $value = (string) $value; - if (!preg_match(static::PATTERN, $value, $matches)) { + \DateTime::createFromFormat($constraint->format, $value); + + $errors = \DateTime::getLastErrors(); + + if (0 < $errors['error_count']) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(DateTime::INVALID_FORMAT_ERROR) @@ -49,18 +57,23 @@ public function validate($value, Constraint $constraint) return; } - if (!DateValidator::checkDate($matches[1], $matches[2], $matches[3])) { - $this->context->buildViolation($constraint->message) - ->setParameter('{{ value }}', $this->formatValue($value)) - ->setCode(DateTime::INVALID_DATE_ERROR) - ->addViolation(); - } - - if (!TimeValidator::checkTime($matches[4], $matches[5], $matches[6])) { - $this->context->buildViolation($constraint->message) - ->setParameter('{{ value }}', $this->formatValue($value)) - ->setCode(DateTime::INVALID_TIME_ERROR) - ->addViolation(); + 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/Tests/Constraints/DateTimeValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php index ebb1f3e6b74f..28206025d54e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php @@ -50,12 +50,30 @@ public function testExpectsStringCompatibleType() $this->validator->validate(new \stdClass(), new DateTime()); } + public function testDateTimeWithDefaultFormat() + { + $this->validator->validate('1995-05-10 19:33:00', new DateTime()); + + $this->assertNoViolation(); + + $this->validator->validate('1995-03-24', new DateTime()); + + $this->buildViolation('This value is not a valid datetime.') + ->setParameter('{{ value }}', '"1995-03-24"') + ->setCode(DateTime::INVALID_FORMAT_ERROR) + ->assertRaised(); + } + /** * @dataProvider getValidDateTimes */ - public function testValidDateTimes($dateTime) + public function testValidDateTimes($format, $dateTime) { - $this->validator->validate($dateTime, new DateTime()); + $constraint = new DateTime(array( + 'format' => $format, + )); + + $this->validator->validate($dateTime, $constraint); $this->assertNoViolation(); } @@ -63,19 +81,22 @@ public function testValidDateTimes($dateTime) public function getValidDateTimes() { return array( - array('2010-01-01 01:02:03'), - array('1955-12-12 00:00:00'), - array('2030-05-31 23:59:59'), + array('Y-m-d H:i:s e', '1995-03-24 00:00:00 UTC'), + array('Y-m-d H:i:s', '2010-01-01 01:02:03'), + array('Y/m/d H:i', '2010/01/01 01:02'), + array('F d, Y', 'December 31, 1999'), + array('d-m-Y', '10-05-1995'), ); } /** * @dataProvider getInvalidDateTimes */ - public function testInvalidDateTimes($dateTime, $code) + public function testInvalidDateTimes($format, $dateTime, $code) { $constraint = new DateTime(array( 'message' => 'myMessage', + 'format' => $format, )); $this->validator->validate($dateTime, $constraint); @@ -89,16 +110,16 @@ public function testInvalidDateTimes($dateTime, $code) public function getInvalidDateTimes() { return array( - array('foobar', DateTime::INVALID_FORMAT_ERROR), - array('2010-01-01', DateTime::INVALID_FORMAT_ERROR), - array('00:00:00', DateTime::INVALID_FORMAT_ERROR), - array('2010-01-01 00:00', DateTime::INVALID_FORMAT_ERROR), - array('2010-13-01 00:00:00', DateTime::INVALID_DATE_ERROR), - array('2010-04-32 00:00:00', DateTime::INVALID_DATE_ERROR), - array('2010-02-29 00:00:00', DateTime::INVALID_DATE_ERROR), - array('2010-01-01 24:00:00', DateTime::INVALID_TIME_ERROR), - array('2010-01-01 00:60:00', DateTime::INVALID_TIME_ERROR), - array('2010-01-01 00:00:60', DateTime::INVALID_TIME_ERROR), + array('Y-m-d', 'foobar', DateTime::INVALID_FORMAT_ERROR), + 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), ); } }