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\Context\ExecutionContextInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
18 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
21 * ChoiceValidator validates that the value is one of the expected values.
23 * @author Fabien Potencier <fabien@symfony.com>
24 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
25 * @author Bernhard Schussek <bschussek@gmail.com>
27 class ChoiceValidator extends ConstraintValidator
32 public function validate($value, Constraint $constraint)
34 if (!$constraint instanceof Choice) {
35 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
38 if (!is_array($constraint->choices) && !$constraint->callback) {
39 throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
42 if (null === $value) {
46 if ($constraint->multiple && !is_array($value)) {
47 throw new UnexpectedTypeException($value, 'array');
50 if ($constraint->callback) {
51 if (!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 ($constraint->multiple) {
62 foreach ($value as $_value) {
63 if (!in_array($_value, $choices, $constraint->strict)) {
64 if ($this->context instanceof ExecutionContextInterface) {
65 $this->context->buildViolation($constraint->multipleMessage)
66 ->setParameter('{{ value }}', $this->formatValue($_value))
67 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
68 ->setInvalidValue($_value)
71 $this->buildViolation($constraint->multipleMessage)
72 ->setParameter('{{ value }}', $this->formatValue($_value))
73 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
74 ->setInvalidValue($_value)
82 $count = count($value);
84 if ($constraint->min !== null && $count < $constraint->min) {
85 if ($this->context instanceof ExecutionContextInterface) {
86 $this->context->buildViolation($constraint->minMessage)
87 ->setParameter('{{ limit }}', $constraint->min)
88 ->setPlural((int) $constraint->min)
89 ->setCode(Choice::TOO_FEW_ERROR)
92 $this->buildViolation($constraint->minMessage)
93 ->setParameter('{{ limit }}', $constraint->min)
94 ->setPlural((int) $constraint->min)
95 ->setCode(Choice::TOO_FEW_ERROR)
102 if ($constraint->max !== null && $count > $constraint->max) {
103 if ($this->context instanceof ExecutionContextInterface) {
104 $this->context->buildViolation($constraint->maxMessage)
105 ->setParameter('{{ limit }}', $constraint->max)
106 ->setPlural((int) $constraint->max)
107 ->setCode(Choice::TOO_MANY_ERROR)
110 $this->buildViolation($constraint->maxMessage)
111 ->setParameter('{{ limit }}', $constraint->max)
112 ->setPlural((int) $constraint->max)
113 ->setCode(Choice::TOO_MANY_ERROR)
119 } elseif (!in_array($value, $choices, $constraint->strict)) {
120 if ($this->context instanceof ExecutionContextInterface) {
121 $this->context->buildViolation($constraint->message)
122 ->setParameter('{{ value }}', $this->formatValue($value))
123 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
126 $this->buildViolation($constraint->message)
127 ->setParameter('{{ value }}', $this->formatValue($value))
128 ->setCode(Choice::NO_SUCH_CHOICE_ERROR)