function DateValidator::validate
Overrides ConstraintValidatorInterface::validate
1 method overrides DateValidator::validate()
- DateTimeValidator::validate in vendor/
symfony/ validator/ Constraints/ DateTimeValidator.php - Checks if the passed value is valid.
File
-
vendor/
symfony/ validator/ Constraints/ DateValidator.php, line 36
Class
- DateValidator
- @author Bernhard Schussek <bschussek@gmail.com>
Namespace
Symfony\Component\Validator\ConstraintsCode
public function validate(mixed $value, Constraint $constraint) : void {
if (!$constraint instanceof Date) {
throw new UnexpectedTypeException($constraint, Date::class);
}
if (null === $value || '' === $value) {
return;
}
if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;
if (!preg_match(static::PATTERN, $value, $matches)) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Date::INVALID_FORMAT_ERROR)
->addViolation();
return;
}
if (!self::checkDate($matches['year'] ?? $matches[1], $matches['month'] ?? $matches[2], $matches['day'] ?? $matches[3])) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Date::INVALID_DATE_ERROR)
->addViolation();
}
}