8000 Label regex in date validator · symfony/symfony@23cb7fc · GitHub
[go: up one dir, main page]

Skip to content

Commit 23cb7fc

Browse files
committed
Label regex in date validator
1 parent 7466148 commit 23cb7fc

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

UPGRADE-5.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ Routing
1111
-------
1212

1313
* Deprecated `RouteCollectionBuilder` in favor of `RoutingConfigurator`.
14+
15+
Validator
16+
---------
17+
18+
* When extending `DateValidator`, if you override the pattern, make sure you label your regex with day, month and year.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
class DateValidator extends ConstraintValidator
2323
{
24-
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2})$/';
24+
const PATTERN = '/^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$/';
2525

2626
/**
2727
* Checks whether a date is valid.
@@ -61,7 +61,9 @@ public function validate($value, Constraint $constraint)
6161
return;
6262
}
6363

64-
if (!self::checkDate($matches[1], $matches[2], $matches[3])) {
64+
if (!isset($matches['year'], $matches['month'], $matches['day'])
65+
|| !self::checkDate($matches['year'], $matches['month'], $matches['day'])
66+
) {
6567
$this->context->buildViolation($constraint->message)
6668
->setParameter('{{ value }}', $this->formatValue($value))
6769
->setCode(Date::INVALID_DATE_ERROR)

0 commit comments

Comments
 (0)
0