From 1aabf9f917b9d4a72e54d44d87b9545ceb2ce40b Mon Sep 17 00:00:00 2001 From: Alexander Volochnev Date: Sun, 21 Apr 2013 16:26:42 +0400 Subject: [PATCH] DateRange constraint added --- .../Validator/Constraints/DateRange.php | 46 ++++++++++++++++ .../Constraints/DateRangeValidator.php | 53 +++++++++++++++++++ .../Resources/translations/validators.en.xlf | 12 +++++ .../Resources/translations/validators.ru.xlf | 12 +++++ 4 files changed, 123 insertions(+) create mode 100644 src/Symfony/Component/Validator/Constraints/DateRange.php create mode 100644 src/Symfony/Component/Validator/Constraints/DateRangeValidator.php diff --git a/src/Symfony/Component/Validator/Constraints/DateRange.php b/src/Symfony/Component/Validator/Constraints/DateRange.php new file mode 100644 index 0000000000000..2afe6403cbb68 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/DateRange.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\Constraints\Range; + +/** + * @Annotation + * + * @api + * + * @author Alexander Volochnev + */ +class DateRange extends Range +{ + public $format = "d/m/Y"; + public $minMessage = 'This date should be {{ limit }} or later.'; + public $maxMessage = 'This date should be {{ limit }} or earlier.'; + public $invalidMessage = 'This value should be a valid date.'; + + public function __construct($options = null) + { + parent::__construct($options); + + if (isset($options['format']) && null !== $options['format']) { + $this->format = $options['format']; + } + + if (null !== $this->min) { + $this->min = \DateTime::createFromFormat($this->format, $this->min); + } + + if (null !== $this->max) { + $this->max = \DateTime::createFromFormat($this->format, $this->max); + } + } +} diff --git a/src/Symfony/Component/Validator/Constraints/DateRangeValidator.php b/src/Symfony/Component/Validator/Constraints/DateRangeValidator.php new file mode 100644 index 0000000000000..86c37e83fd662 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/DateRangeValidator.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Constraints; + +use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Constraint; + +/** + * @author Alexander Volochnev + */ +class DateRangeValidator extends ConstraintValidator +{ + /** + * {@inheritDoc} + */ + public function validate($value, Constraint $constraint) + { + if (null === $value) { + return; + } + + if (!$value instanceof \DateTime) { + $this->context->addViolation($constraint->invalidMessage); + + return; + } + + if (null !== $constraint->max && $value > $constraint->max) { + $this->context->addViolation($constraint->maxMessage, array( + '{{ value }}' => $value->format($constraint->format), + '{{ limit }}' => $constraint->max->format($constraint->format), + )); + + return; + } + + if (null !== $constraint->min && $value < $constraint->min) { + $this->context->addViolation($constraint->minMessage, array( + '{{ value }}' => $value->format($constraint->format), + '{{ limit }}' => $constraint->min->format($constraint->format), + )); + } + } +} diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf index abb849d7b7894..a46ded8082306 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf @@ -242,6 +242,18 @@ This value is not a valid ISSN. This value is not a valid ISSN. + + This value should be a valid date. + This value should be a valid date. + + + This date should be {{ limit }} or earlier. + This date should be {{ limit }} or earlier. + + + This value should be {{ limit }} or later. + This value should be {{ limit }} or later. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf index 3b296c9c4d7b3..bdd49f24e3d75 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf @@ -238,6 +238,18 @@ This value is neither a valid ISBN-10 nor a valid ISBN-13. Значение не соответствует форматам ISBN-10 и ISBN-13 + + This value should be a valid date. + Значение должно быть датой. + + + This date should be {{ limit }} or earlier. + Дата должна быть {{ limit }} или ранее. + + + This value should be {{ limit }} or later. + Дата должна быть {{ limit }} или позднее. +