4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
20 * ChoiceValidator validates that the value is one of the expected values.
22 * @author Fabien Potencier <fabien@symfony.com>
23 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
24 * @author Bernhard Schussek <bschussek@gmail.com>
26 class ChoiceValidator extends ConstraintValidator
31 public function validate($value, Constraint $constraint)
33 if (!$constraint instanceof Choice) {
34 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
37 if (!is_array($constraint->choices) && !$constraint->callback) {
38 throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
41 if (null === $value) {
45 if ($constraint->multiple && !is_array($value)) {
46 throw new UnexpectedTypeException($value, 'array');
49 if ($constraint->callback) {
50 if (!is_callable($choices = array($this->context->getObject(), $constraint->callback))
51 && !is_callable($choices = array($this->context->getClassName(), $constraint->callback))
52 && !is_callable($choices = $constraint->callback)
54 throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');
56 $choices = call_user_func($choices);
58 $choices = $constraint->choices;
61 if (true !== $constraint->strict) {
62 @trigger_error('Not setting the strict option of the Choice constraint to true is deprecated since Symfony 3.4 and will throw an exception in 4.0.', E_USER_DEPRECATED);
65 if ($constraint->multiple) {
66 foreach ($value as $_value) {
67 if (!in_array($_value, $choices, $constraint->strict)) {
68 $this->context->buildViolation($constraint->multipleMessage)
69 ->setParameter('{{ value }}', $this->formatValue($_value))
70 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
71 ->setInvalidValue($_value)
78 $count = count($value);
80 if (null !== $constraint->min && $count < $constraint->min) {
81 $this->context->buildViolation($constraint->minMessage)
82 ->setParameter('{{ limit }}', $constraint->min)
83 ->setPlural((int) $constraint->min)
84 ->setCode(Choice::TOO_FEW_ERROR)
90 if (null !== $constraint->max && $count > $constraint->max) {
91 $this->context->buildViolation($constraint->maxMessage)
92 ->setParameter('{{ limit }}', $constraint->max)
93 ->setPlural((int) $constraint->max)
94 ->setCode(Choice::TOO_MANY_ERROR)
99 } elseif (!in_array($value, $choices, $constraint->strict)) {
100 $this->context->buildViolation($constraint->message)
101 ->setParameter('{{ value }}', $this->formatValue($value))
102 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)