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

Breadcrumb

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

class UniqueValidator

@author Yevgeniy Zholkevskiy <zhenya.zholkevskiy@gmail.com>

Hierarchy

  • class \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface
    • class \Symfony\Component\Validator\Constraints\UniqueValidator extends \Symfony\Component\Validator\ConstraintValidator

Expanded class hierarchy of UniqueValidator

File

vendor/symfony/validator/Constraints/UniqueValidator.php, line 22

Namespace

Symfony\Component\Validator\Constraints
View source
class UniqueValidator extends ConstraintValidator {
    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;
        }
    }
    private function getNormalizer(Unique $unique) : callable {
        return $unique->normalizer ?? static fn($value) => $value;
    }
    private function reduceElementKeys(array $fields, array $element) : array {
        $output = [];
        foreach ($fields as $field) {
            if (!\is_string($field)) {
                throw new UnexpectedTypeException($field, 'string');
            }
            if (\array_key_exists($field, $element)) {
                $output[$field] = $element[$field];
            }
        }
        return $output;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ConstraintValidator::$context protected property
ConstraintValidator::formatTypeOf protected function Returns a string representation of the type of the value.
ConstraintValidator::formatValue protected function Returns a string representation of the value.
ConstraintValidator::formatValues protected function Returns a string representation of a list of values.
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize
ConstraintValidator::OBJECT_TO_STRING public constant Whether to cast objects with a &quot;__toString()&quot; method to strings.
ConstraintValidator::PRETTY_DATE public constant Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter}
(if it is available) or as RFC-3339 dates (&quot;Y-m-d H:i:s&quot;).
UniqueValidator::getNormalizer private function
UniqueValidator::reduceElementKeys private function
UniqueValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
RSS feed
Powered by Drupal