Skip to content

Commit 9e94c9f

Browse files
committed
Added a format option to the DateTime constraint.
1 parent 7848a46 commit 9e94c9f

File tree

6 files changed

+81
-29
lines changed

6 files changed

+81
-29
lines changed

UPGRADE-3.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,9 @@ Yaml
112112

113113
* The `!!php/object` tag to indicate dumped PHP objects has been deprecated
114114
and will be removed in Symfony 4.0. Use the `!php/object` tag instead.
115+
116+
Validator
117+
---------
118+
119+
* The `DateTimeValidator::PATTERN` constant is deprecated and will be removed in
120+
Symfony 4.0.

UPGRADE-4.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,8 @@ Yaml
109109

110110
* The `!!php/object` tag to indicate dumped PHP objects was removed in favor of
111111
the `!php/object` tag.
112+
113+
Validator
114+
---------
115+
116+
* The `DateTimeValidator::PATTERN` constant was removed.

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
3.1.0
5+
-----
6+
7+
* deprecated `DateTimeValidator::PATTERN` constant
8+
* added a `format` option to the `DateTime` constraint
9+
410
2.8.0
511
-----
612

src/Symfony/Component/Validator/Constraints/DateTime.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ class DateTime extends Constraint
3131
self::INVALID_TIME_ERROR => 'INVALID_TIME_ERROR',
3232
);
3333

34+
public $format = 'Y-m-d H:i:s';
3435
public $message = 'This value is not a valid datetime.';
3536
}

src/Symfony/Component/Validator/Constraints/DateTimeValidator.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616

1717
/**
1818
* @author Bernhard Schussek <bschussek@gmail.com>
19+
* @author Diego Saint Esteben <diego@saintesteben.me>
1920
*/
2021
class DateTimeValidator extends DateValidator
2122
{
23+
/**
24+
* @deprecated since version 3.1, to be removed in 4.0.
25+
*/
2226
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/';
2327

2428
/**
@@ -40,7 +44,11 @@ public function validate($value, Constraint $constraint)
4044

4145
$value = (string) $value;
4246

43-
if (!preg_match(static::PATTERN, $value, $matches)) {
47+
\DateTime::createFromFormat($constraint->format, $value);
48+
49+
$errors = \DateTime::getLastErrors();
50+
51+
if (0 < $errors['error_count']) {
4452
$this->context->buildViolation($constraint->message)
4553
->setParameter('{{ value }}', $this->formatValue($value))
4654
->setCode(DateTime::INVALID_FORMAT_ERROR)
@@ -49,18 +57,23 @@ public function validate($value, Constraint $constraint)
4957
return;
5058
}
5159

52-
if (!DateValidator::checkDate($matches[1], $matches[2], $matches[3])) {
53-
$this->context->buildViolation($constraint->message)
54-
->setParameter('{{ value }}', $this->formatValue($value))
55-
->setCode(DateTime::INVALID_DATE_ERROR)
56-
->addViolation();
57-
}
58-
59-
if (!TimeValidator::checkTime($matches[4], $matches[5], $matches[6])) {
60-
$this->context->buildViolation($constraint->message)
61-
->setParameter('{{ value }}', $this->formatValue($value))
62-
->setCode(DateTime::INVALID_TIME_ERROR)
63-
->addViolation();
60+
foreach ($errors['warnings'] as $warning) {
61+
if ('The parsed date was invalid' === $warning) {
62+
$this->context->buildViolation($constraint->message)
63+
->setParameter('{{ value }}', $this->formatValue($value))
64+
->setCode(DateTime::INVALID_DATE_ERROR)
65+
->addViolation();
66+
} elseif ('The parsed time was invalid' === $warning) {
67+
$this->context->buildViolation($constraint->message)
68+
->setParameter('{{ value }}', $this->formatValue($value))
69+
->setCode(DateTime::INVALID_TIME_ERROR)
70+
->addViolation();
71+
} else {
72+
$this->context->buildViolation($constraint->message)
73+
->setParameter('{{ value }}', $this->formatValue($value))
74+
->setCode(DateTime::INVALID_FORMAT_ERROR)
75+
->addViolation();
76+
}
6477
}
6578
}
6679
}

src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,53 @@ public function testExpectsStringCompatibleType()
5050
$this->validator->validate(new \stdClass(), new DateTime());
5151
}
5252

53+
public function testDateTimeWithDefaultFormat()
54+
{
55+
$this->validator->validate('1995-05-10 19:33:00', new DateTime());
56+
57+
$this->assertNoViolation();
58+
59+
$this->validator->validate('1995-03-24', new DateTime());
60+
61+
$this->buildViolation('This value is not a valid datetime.')
62+
->setParameter('{{ value }}', '"1995-03-24"')
63+
->setCode(DateTime::INVALID_FORMAT_ERROR)
64+
->assertRaised();
65+
}
66+
5367
/**
5468
* @dataProvider getValidDateTimes
5569
*/
56-
public function testValidDateTimes($dateTime)
70+
public function testValidDateTimes($format, $dateTime)
5771
{
58-
$this->validator->validate($dateTime, new DateTime());
72+
$constraint = new DateTime(array(
73+
'format' => $format,
74+
));
75+
76+
$this->validator->validate($dateTime, $constraint);
5977

6078
$this->assertNoViolation();
6179
}
6280

6381
public function getValidDateTimes()
6482
{
6583
return array(
66-
array('2010-01-01 01:02:03'),
67-
array('1955-12-12 00:00:00'),
68-
array('2030-05-31 23:59:59'),
84+
array('Y-m-d H:i:s e', '1995-03-24 00:00:00 UTC'),
85+
array('Y-m-d H:i:s', '2010-01-01 01:02:03'),
86+
array('Y/m/d H:i', '2010/01/01 01:02'),
87+
array('F d, Y', 'December 31, 1999'),
88+
array('d-m-Y', '10-05-1995'),
6989
);
7090
}
7191

7292
/**
7393
* @dataProvider getInvalidDateTimes
7494
*/
75-
public function testInvalidDateTimes($dateTime, $code)
95+
public function testInvalidDateTimes($format, $dateTime, $code)
7696
{
7797
$constraint = new DateTime(array(
7898
'message' => 'myMessage',
99+
'format' => $format,
79100
));
80101

81102
$this->validator->validate($dateTime, $constraint);
@@ -89,16 +110,16 @@ public function testInvalidDateTimes($dateTime, $code)
89110
public function getInvalidDateTimes()
90111
{
91112
return array(
92-
array('foobar', DateTime::INVALID_FORMAT_ERROR),
93-
array('2010-01-01', DateTime::INVALID_FORMAT_ERROR),
94-
array('00:00:00', DateTime::INVALID_FORMAT_ERROR),
95-
array('2010-01-01 00:00', DateTime::INVALID_FORMAT_ERROR),
96-
array('2010-13-01 00:00:00', DateTime::INVALID_DATE_ERROR),
97-
array('2010-04-32 00:00:00', DateTime::INVALID_DATE_ERROR),
98-
array('2010-02-29 00:00:00', DateTime::INVALID_DATE_ERROR),
99-
array('2010-01-01 24:00:00', DateTime::INVALID_TIME_ERROR),
100-
array('2010-01-01 00:60:00', DateTime::INVALID_TIME_ERROR),
101-
array('2010-01-01 00:00:60', DateTime::INVALID_TIME_ERROR),
113+
array('Y-m-d', 'foobar', DateTime::INVALID_FORMAT_ERROR),
114+
array('H:i', '00:00:00', DateTime::INVALID_FORMAT_ERROR),
115+
array('Y-m-d', '2010-01-01 00:00', DateTime::INVALID_FORMAT_ERROR),
116+
array('Y-m-d e', '2010-01-01 TCU', DateTime::INVALID_FORMAT_ERROR),
117+
array('Y-m-d H:i:s', '2010-13-01 00:00:00', DateTime::INVALID_DATE_ERROR),
118+
array('Y-m-d H:i:s', '2010-04-32 00:00:00', DateTime::INVALID_DATE_ERROR),
119+
array('Y-m-d H:i:s', '2010-02-29 00:00:00', DateTime::INVALID_DATE_ERROR),
120+
array('Y-m-d H:i:s', '2010-01-01 24:00:00', DateTime::INVALID_TIME_ERROR),
121+
array('Y-m-d H:i:s', '2010-01-01 00:60:00', DateTime::INVALID_TIME_ERROR),
122+
array('Y-m-d H:i:s', '2010-01-01 00:00:60', DateTime::INVALID_TIME_ERROR),
102123
);
103124
}
104125
}

0 commit comments

Comments
 (0)