-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Validator] Add "format" option to DateTime constraint #14538
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 |
---|---|---|
|
@@ -17,11 +17,15 @@ | |
|
||
/** | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
* @author Radu Murzea <radu.murzea@gmail.com> | ||
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. Same here |
||
* | ||
* @api | ||
*/ | ||
class DateTimeValidator extends DateValidator | ||
{ | ||
/** | ||
* @deprecated since version 2.8, to be removed in 3.0. | ||
*/ | ||
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/'; | ||
|
||
/** | ||
|
@@ -43,48 +47,79 @@ public function validate($value, Constraint $constraint) | |
|
||
$value = (string) $value; | ||
|
||
if (!preg_match(static::PATTERN, $value, $matches)) { | ||
$dateTimeObject = \DateTime::createFromFormat($constraint->format, $value); | ||
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. Assign a variable here is not necessary |
||
$errors = \DateTime::getLastErrors(); | ||
|
||
//the value was successfully parsed | ||
if ($errors['error_count'] + $errors['warning_count'] <= 0) { | ||
return; | ||
} | ||
|
||
//see the function's phpdoc for details | ||
$errorCode = $this->getViolationCode($errors, 'errors'); | ||
|
||
if (! is_null($errorCode)) { | ||
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. Extra space |
||
if ($this->context instanceof ExecutionContextInterface) { | ||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(DateTime::INVALID_FORMAT_ERROR) | ||
->setCode($errorCode) | ||
->addViolation(); | ||
} else { | ||
$this->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(DateTime::INVALID_FORMAT_ERROR) | ||
->setCode($errorCode) | ||
->addViolation(); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (!DateValidator::checkDate($matches[1], $matches[2], $matches[3])) { | ||
//see the function's phpdoc for details | ||
$warningCode = $this->getViolationCode($errors, 'warnings'); | ||
|
||
if (! is_null($warningCode)) { | ||
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. Extra space |
||
if ($this->context instanceof ExecutionContextInterface) { | ||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(DateTime::INVALID_DATE_ERROR) | ||
->setCode($warningCode) | ||
->addViolation(); | ||
} else { | ||
$this->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(DateTime::INVALID_DATE_ERROR) | ||
->setCode($warningCode) | ||
->addViolation(); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
/** | ||
* PHP's DateTime stores the errors and warning at unpredictable indexes, | ||
* so it's a bit trickier to retrieve them (we have to use array_values + array_shift). | ||
* | ||
* Also, based on the error/warning message string, we can guess if it was | ||
* a date problem, time problem or format/general problem. | ||
* | ||
* @param array $errors result from calling \DateTime::getLastErrors() | ||
* @param string $key the key under which to search for errors/warnings | ||
* @return mixed an integer representing the error code or NULL if everything was ok | ||
*/ | ||
private function getViolationCode($errors, $key) | ||
{ | ||
if (isset($errors[$key]) && count($errors[$key]) > 0) { | ||
$allErrors = array_values($errors[$key]); | ||
$firstError = array_shift($allErrors); | ||
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. Can be more than one error |
||
|
||
if (!TimeValidator::checkTime($matches[4], $matches[5], $matches[6])) { | ||
if ($this->context instanceof ExecutionContextInterface) { | ||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(DateTime::INVALID_TIME_ERROR) | ||
->addViolation(); | ||
if (strpos($firstError, 'date') !== false) { | ||
return DateTime::INVALID_DATE_ERROR; | ||
} elseif (strpos($firstError, 'time') !== false) { | ||
return DateTime::INVALID_TIME_ERROR; | ||
} else { | ||
$this->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(DateTime::INVALID_TIME_ERROR) | ||
->addViolation(); | ||
return DateTime::INVALID_FORMAT_ERROR; | ||
} | ||
} else { | ||
return; | ||
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 |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
use Symfony\Component\Validator\Constraints\DateValidator; | ||
use Symfony\Component\Validator\Validation; | ||
|
||
/** | ||
* @deprecated since version 2.8, to be removed in 3.0. | ||
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. Missing |
||
*/ | ||
class DateValidatorTest extends AbstractConstraintValidatorTest | ||
{ | ||
protected function getApiVersion() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ | |
use Symfony\Component\Validator\Constraints\TimeValidator; | ||
use Symfony\Component\Validator\Validation; | ||
|
||
/** | ||
* @deprecated since version 2.8, to be removed in 3.0. | ||
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. Same here |
||
*/ | ||
class TimeValidatorTest extends AbstractConstraintValidatorTest | ||
{ | ||
protected function getApiVersion() | ||
|
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.
You are not the author