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\UnexpectedTypeException;
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class RangeValidator extends ConstraintValidator
27 public function validate($value, Constraint $constraint)
29 if (!$constraint instanceof Range) {
30 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Range');
33 if (null === $value) {
37 if (!is_numeric($value) && !$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
38 if ($this->context instanceof ExecutionContextInterface) {
39 $this->context->buildViolation($constraint->invalidMessage)
40 ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
41 ->setCode(Range::INVALID_CHARACTERS_ERROR)
44 $this->buildViolation($constraint->invalidMessage)
45 ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
46 ->setCode(Range::INVALID_CHARACTERS_ERROR)
53 $min = $constraint->min;
54 $max = $constraint->max;
56 // Convert strings to DateTimes if comparing another DateTime
57 // This allows to compare with any date/time value supported by
58 // the DateTime constructor:
59 // http://php.net/manual/en/datetime.formats.php
60 if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
61 if (is_string($min)) {
62 $min = new \DateTime($min);
65 if (is_string($max)) {
66 $max = new \DateTime($max);
70 if (null !== $constraint->max && $value > $max) {
71 if ($this->context instanceof ExecutionContextInterface) {
72 $this->context->buildViolation($constraint->maxMessage)
73 ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
74 ->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
75 ->setCode(Range::TOO_HIGH_ERROR)
78 $this->buildViolation($constraint->maxMessage)
79 ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
80 ->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
81 ->setCode(Range::TOO_HIGH_ERROR)
88 if (null !== $constraint->min && $value < $min) {
89 if ($this->context instanceof ExecutionContextInterface) {
90 $this->context->buildViolation($constraint->minMessage)
91 ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
92 ->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
93 ->setCode(Range::TOO_LOW_ERROR)
96 $this->buildViolation($constraint->minMessage)
97 ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
98 ->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
99 ->setCode(Range::TOO_LOW_ERROR)