function RegexValidator::validate
Overrides ConstraintValidatorInterface::validate
File
-
vendor/
symfony/ validator/ Constraints/ RegexValidator.php, line 27
Class
- RegexValidator
- Validates whether a value match or not given regexp pattern.
Namespace
Symfony\Component\Validator\ConstraintsCode
public function validate(mixed $value, Constraint $constraint) : void {
if (!$constraint instanceof Regex) {
throw new UnexpectedTypeException($constraint, Regex::class);
}
if (null === $value || '' === $value) {
return;
}
if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}
$value = (string) $value;
if (null !== $constraint->normalizer) {
$value = ($constraint->normalizer)($value);
}
if ($constraint->match xor preg_match($constraint->pattern, $value)) {
$this->context
->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ pattern }}', $constraint->pattern)
->setCode(Regex::REGEX_FAILED_ERROR)
->addViolation();
}
}