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

Breadcrumb

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

class ChoiceValidator

ChoiceValidator validates that the value is one of the expected values.

@author Fabien Potencier <fabien@symfony.com> @author Florian Eckerstorfer <florian@eckerstorfer.org> @author Bernhard Schussek <bschussek@gmail.com>

Hierarchy

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

Expanded class hierarchy of ChoiceValidator

1 file declares its use of ChoiceValidator
AllowedValuesConstraintValidator.php in core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraintValidator.php

File

vendor/symfony/validator/Constraints/ChoiceValidator.php, line 28

Namespace

Symfony\Component\Validator\Constraints
View source
class ChoiceValidator extends ConstraintValidator {
    public function validate(mixed $value, Constraint $constraint) : void {
        if (!$constraint instanceof Choice) {
            throw new UnexpectedTypeException($constraint, Choice::class);
        }
        if (!\is_array($constraint->choices) && !$constraint->callback) {
            throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice.');
        }
        if (null === $value) {
            return;
        }
        if ($constraint->multiple && !\is_array($value)) {
            throw new UnexpectedValueException($value, 'array');
        }
        if ($constraint->callback) {
            if (!\is_callable($choices = [
                $this->context
                    ->getObject(),
                $constraint->callback,
            ]) && !\is_callable($choices = [
                $this->context
                    ->getClassName(),
                $constraint->callback,
            ]) && !\is_callable($choices = $constraint->callback)) {
                throw new ConstraintDefinitionException('The Choice constraint expects a valid callback.');
            }
            $choices = $choices();
            if (!is_array($choices)) {
                throw new ConstraintDefinitionException(sprintf('The Choice constraint callback "%s" is expected to return an array, but returned "%s".', trim($this->formatValue($constraint->callback), '"'), get_debug_type($choices)));
            }
        }
        else {
            $choices = $constraint->choices;
        }
        if (true !== $constraint->strict) {
            throw new RuntimeException('The "strict" option of the Choice constraint should not be used.');
        }
        if ($constraint->multiple) {
            foreach ($value as $_value) {
                if ($constraint->match xor \in_array($_value, $choices, true)) {
                    $this->context
                        ->buildViolation($constraint->multipleMessage)
                        ->setParameter('{{ value }}', $this->formatValue($_value))
                        ->setParameter('{{ choices }}', $this->formatValues($choices))
                        ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
                        ->setInvalidValue($_value)
                        ->addViolation();
                    return;
                }
            }
            $count = \count($value);
            if (null !== $constraint->min && $count < $constraint->min) {
                $this->context
                    ->buildViolation($constraint->minMessage)
                    ->setParameter('{{ limit }}', $constraint->min)
                    ->setPlural($constraint->min)
                    ->setCode(Choice::TOO_FEW_ERROR)
                    ->addViolation();
                return;
            }
            if (null !== $constraint->max && $count > $constraint->max) {
                $this->context
                    ->buildViolation($constraint->maxMessage)
                    ->setParameter('{{ limit }}', $constraint->max)
                    ->setPlural($constraint->max)
                    ->setCode(Choice::TOO_MANY_ERROR)
                    ->addViolation();
            }
        }
        elseif ($constraint->match xor \in_array($value, $choices, true)) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setParameter('{{ choices }}', $this->formatValues($choices))
                ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
                ->addViolation();
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ChoiceValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate 1
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;).
RSS feed
Powered by Drupal