function UniqueValidator::validate
Overrides ConstraintValidatorInterface::validate
File
-
vendor/
symfony/ validator/ Constraints/ UniqueValidator.php, line 24
Class
- UniqueValidator
- @author Yevgeniy Zholkevskiy <zhenya.zholkevskiy@gmail.com>
Namespace
Symfony\Component\Validator\ConstraintsCode
public function validate(mixed $value, Constraint $constraint) : void {
if (!$constraint instanceof Unique) {
throw new UnexpectedTypeException($constraint, Unique::class);
}
$fields = (array) $constraint->fields;
if (null === $value) {
return;
}
if (!\is_array($value) && !$value instanceof \IteratorAggregate) {
throw new UnexpectedValueException($value, 'array|IteratorAggregate');
}
$collectionElements = [];
$normalizer = $this->getNormalizer($constraint);
foreach ($value as $index => $element) {
$element = $normalizer($element);
if ($fields && !($element = $this->reduceElementKeys($fields, $element))) {
continue;
}
if (\in_array($element, $collectionElements, true)) {
$this->context
->buildViolation($constraint->message)
->atPath("[{$index}]" . (null !== $constraint->errorPath ? ".{$constraint->errorPath}" : ''))
->setParameter('{{ value }}', $this->formatValue($element))
->setCode(Unique::IS_NOT_UNIQUE)
->addViolation();
return;
}
$collectionElements[] = $element;
}
}