Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. UniqueValidator.php

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\Constraints

Code

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;
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal