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>

@api

Hierarchy

Expanded class hierarchy of ChoiceValidator

1 file declares its use of ChoiceValidator
ChoiceValidatorTest.php in drupal/core/vendor/symfony/validator/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php

File

drupal/core/vendor/symfony/validator/Symfony/Component/Validator/Constraints/ChoiceValidator.php, line 28

Namespace

Symfony\Component\Validator\Constraints
View source
class ChoiceValidator extends ConstraintValidator {

  /**
   * {@inheritDoc}
   */
  public function validate($value, Constraint $constraint) {
    if (!$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 UnexpectedTypeException($value, 'array');
    }
    if ($constraint->callback) {
      if (is_callable(array(
        $this->context
          ->getClassName(),
        $constraint->callback,
      ))) {
        $choices = call_user_func(array(
          $this->context
            ->getClassName(),
          $constraint->callback,
        ));
      }
      elseif (is_callable($constraint->callback)) {
        $choices = call_user_func($constraint->callback);
      }
      else {
        throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');
      }
    }
    else {
      $choices = $constraint->choices;
    }
    if ($constraint->multiple) {
      foreach ($value as $_value) {
        if (!in_array($_value, $choices, $constraint->strict)) {
          $this->context
            ->addViolation($constraint->multipleMessage, array(
            '{{ value }}' => $_value,
          ));
        }
      }
      $count = count($value);
      if ($constraint->min !== null && $count < $constraint->min) {
        $this->context
          ->addViolation($constraint->minMessage, array(
          '{{ limit }}' => $constraint->min,
        ), null, (int) $constraint->min);
        return;
      }
      if ($constraint->max !== null && $count > $constraint->max) {
        $this->context
          ->addViolation($constraint->maxMessage, array(
          '{{ limit }}' => $constraint->max,
        ), null, (int) $constraint->max);
        return;
      }
    }
    elseif (!in_array($value, $choices, $constraint->strict)) {
      $this->context
        ->addViolation($constraint->message, array(
        '{{ value }}' => $value,
      ));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChoiceValidator::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
ConstraintValidator::$context protected property
ConstraintValidator::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize 1