diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md
index 88beb336c9..9170f96c93 100644
--- a/src/Symfony/Component/Validator/CHANGELOG.md
+++ b/src/Symfony/Component/Validator/CHANGELOG.md
@@ -18,6 +18,8 @@ CHANGELOG
* added the `min_limit_path` and `max_limit_path` parameters in violations when using
`Range` constraint with respectively the `minPropertyPath` and
`maxPropertyPath` options.
+ * added a new `notInRangeMessage` options to the `Range` constraint that will
+ be used in the violation builder when both `min` and `max` are not null.
4.3.0
-----
diff --git a/src/Symfony/Component/Validator/Constraints/Range.php b/src/Symfony/Component/Validator/Constraints/Range.php
index 115b9014d5..9f05edf677 100644
--- a/src/Symfony/Component/Validator/Constraints/Range.php
+++ b/src/Symfony/Component/Validator/Constraints/Range.php
@@ -26,15 +26,18 @@ use Symfony\Component\Validator\Exception\MissingOptionsException;
class Range extends Constraint
{
const INVALID_CHARACTERS_ERROR = 'ad9a9798-7a99-4df7-8ce9-46e416a1e60b';
+ const NOT_IN_RANGE_ERROR = '04b91c99-a946-4221-afc5-e65ebac401eb';
const TOO_HIGH_ERROR = '2d28afcb-e32e-45fb-a815-01c431a86a69';
const TOO_LOW_ERROR = '76454e69-502c-46c5-9643-f447d837c4d5';
protected static $errorNames = [
self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
+ self::NOT_IN_RANGE_ERROR => 'NOT_IN_RANGE_ERROR',
self::TOO_HIGH_ERROR => 'TOO_HIGH_ERROR',
self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
];
+ public $notInRangeMessage = 'This value should be between {{ min }} and {{ max }}.';
public $minMessage = 'This value should be {{ limit }} or more.';
public $maxMessage = 'This value should be {{ limit }} or less.';
public $invalidMessage = 'This value should be a valid number.';
diff --git a/src/Symfony/Component/Validator/Constraints/RangeValidator.php b/src/Symfony/Component/Validator/Constraints/RangeValidator.php
index 3837b0d6fa..08d6f0e38e 100644
--- a/src/Symfony/Component/Validator/Constraints/RangeValidator.php
+++ b/src/Symfony/Component/Validator/Constraints/RangeValidator.php
@@ -70,7 +70,30 @@ class RangeValidator extends ConstraintValidator
}
}
- if (null !== $max && $value > $max) {
+ $hasLowerLimit = null !== $min;
+ $hasUpperLimit = null !== $max;
+
+ if ($hasLowerLimit && $hasUpperLimit && ($value < $min || $value > $max)) {
+ $violationBuilder = $this->context->buildViolation($constraint->notInRangeMessage)
+ ->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
+ ->setParameter('{{ min }}', $this->formatValue($min, self::PRETTY_DATE))
+ ->setParameter('{{ max }}', $this->formatValue($max, self::PRETTY_DATE))
+ ->setCode(Range::NOT_IN_RANGE_ERROR);
+
+ if (null !== $constraint->maxPropertyPath) {
+ $violationBuilder->setParameter('{{ max_limit_path }}', $constraint->maxPropertyPath);
+ }
+
+ if (null !== $constraint->minPropertyPath) {
+ $violationBuilder->setParameter('{{ min_limit_path }}', $constraint->minPropertyPath);
+ }
+
+ $violationBuilder->addViolation();
+
+ return;
+ }
+
+ if ($hasUpperLimit && $value > $max) {
$violationBuilder = $this->context->buildViolation($constraint->maxMessage)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
@@ -89,7 +112,7 @@ class RangeValidator extends ConstraintValidator
return;
}
- if (null !== $min && $value < $min) {
+ if ($hasLowerLimit && $value < $min) {
$violationBuilder = $this->context->buildViolation($constraint->minMessage)
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))
diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf
index d5d9d20997..100d552076 100644
--- a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf
+++ b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf
@@ -362,6 +362,10 @@
This password has been leaked in a data breach, it must not be used. Please use another password.
+
+
+ This value should be between {{ min }} and {{ max }}.
+