-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Validator] Added a format option to the DateTime constraint. #17553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,13 @@ | |
|
||
/** | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
* @author Diego Saint Esteben <diego@saintesteben.me> | ||
*/ | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks very fragile to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, but given the return value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As long as we have tests that validate this behavior across multiple platforms and PHP versions I think that this should be fine. |
||
$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(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add an |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
must be added to the upgrade file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done