Skip to content

[Validator] Add a new constraint message when there is both min and max #32435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ CHANGELOG
* added the `limit_path` parameter in violations when using
`Range` constraint with the `minPropertyPath` or
`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
-----
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@
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.';
Expand Down
27 changes: 25 additions & 2 deletions src/Symfony/Component/Validator/Constraints/RangeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,30 @@ public function validate($value, Constraint $constraint)
}
}

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))
Expand All @@ -89,7 +112,7 @@ public function validate($value, Constraint $constraint)
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@
<source>This password has been leaked in a data breach, it must not be used. Please use another password.</source>
<target>This password has been leaked in a data breach, it must not be used. Please use another password.</target>
</trans-unit>
<trans-unit id="94">
<source>This value should be between {{ min }} and {{ max }}.</source>
<target>This value should be between {{ min }} and {{ max }}.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@
<source>This password has been leaked in a data breach, it must not be used. Please use another password.</source>
<target>Ce mot de passe a été divulgué lors d'une fuite de données, il ne doit plus être utilisé. Veuillez utiliser un autre mot de passe.</target>
</trans-unit>
<trans-unit id="94">
<source>This value should be between {{ min }} and {{ max }}.</source>
<target>Cette valeur doit être comprise entre {{ min }} et {{ max }}.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ public function testInvalidValuesCombinedMax($value, $formattedValue)
$constraint = new Range([
'min' => 10,
'max' => 20,
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
'notInRangeMessage' => 'myNotInRangeMessage',
]);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMaxMessage')
$this->buildViolation('myNotInRangeMessage')
->setParameter('{{ value }}', $formattedValue)
->setParameter('{{ limit }}', 20)
->setCode(Range::TOO_HIGH_ERROR)
->setParameter('{{ min }}', 10)
->setParameter('{{ max }}', 20)
->setCode(Range::NOT_IN_RANGE_ERROR)
->assertRaised();
}

Expand All @@ -164,16 +164,16 @@ public function testInvalidValuesCombinedMin($value, $formattedValue)
$constraint = new Range([
'min' => 10,
'max' => 20,
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
'notInRangeMessage' => 'myNotInRangeMessage',
]);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMinMessage')
$this->buildViolation('myNotInRangeMessage')
->setParameter('{{ value }}', $formattedValue)
->setParameter('{{ limit }}', 10)
->setCode(Range::TOO_LOW_ERROR)
->setParameter('{{ min }}', 10)
->setParameter('{{ max }}', 20)
->setCode(Range::NOT_IN_RANGE_ERROR)
->assertRaised();
}

Expand Down Expand Up @@ -327,16 +327,16 @@ public function testInvalidDatesCombinedMax($value, $dateTimeAsString)
$constraint = new Range([
'min' => 'March 10, 2014',
'max' => 'March 20, 2014',
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
'notInRangeMessage' => 'myNotInRangeMessage',
]);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMaxMessage')
$this->buildViolation('myNotInRangeMessage')
->setParameter('{{ value }}', $dateTimeAsString)
->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
->setCode(Range::TOO_HIGH_ERROR)
->setParameter('{{ min }}', 'Mar 10, 2014, 12:00 AM')
->setParameter('{{ max }}', 'Mar 20, 2014, 12:00 AM')
->setCode(Range::NOT_IN_RANGE_ERROR)
->assertRaised();
}

Expand All @@ -352,16 +352,16 @@ public function testInvalidDatesCombinedMin($value, $dateTimeAsString)
$constraint = new Range([
'min' => 'March 10, 2014',
'max' => 'March 20, 2014',
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
'notInRangeMessage' => 'myNotInRangeMessage',
]);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMinMessage')
$this->buildViolation('myNotInRangeMessage')
->setParameter('{{ value }}', $dateTimeAsString)
->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
->setCode(Range::TOO_LOW_ERROR)
->setParameter('{{ min }}', 'Mar 10, 2014, 12:00 AM')
->setParameter('{{ max }}', 'Mar 20, 2014, 12:00 AM')
->setCode(Range::NOT_IN_RANGE_ERROR)
->assertRaised();
}

Expand Down Expand Up @@ -527,18 +527,18 @@ public function testInvalidValuesCombinedMaxPropertyPath($value, $formattedValue
$constraint = new Range([
'minPropertyPath' => 'min',
'maxPropertyPath' => 'max',
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
'notInRangeMessage' => 'myNotInRangeMessage',
]);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMaxMessage')
$this->buildViolation('myNotInRangeMessage')
->setParameter('{{ value }}', $formattedValue)
->setParameter('{{ limit }}', 20)
->setParameter('{{ min }}', 10)
->setParameter('{{ max }}', 20)
->setParameter('{{ max_limit_path }}', 'max')
->setParameter('{{ min_limit_path }}', 'min')
->setCode(Range::TOO_HIGH_ERROR)
->setCode(Range::NOT_IN_RANGE_ERROR)
->assertRaised();
}

Expand All @@ -552,18 +552,18 @@ public function testInvalidValuesCombinedMinPropertyPath($value, $formattedValue
$constraint = new Range([
'minPropertyPath' => 'min',
'maxPropertyPath' => 'max',
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
'notInRangeMessage' => 'myNotInRangeMessage',
]);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMinMessage')
$this->buildViolation('myNotInRangeMessage')
->setParameter('{{ value }}', $formattedValue)
->setParameter('{{ limit }}', 10)
->setParameter('{{ min }}', 10)
->setParameter('{{ max }}', 20)
->setParameter('{{ max_limit_path }}', 'max')
->setParameter('{{ min_limit_path }}', 'min')
->setCode(Range::TOO_LOW_ERROR)
->setCode(Range::NOT_IN_RANGE_ERROR)
->assertRaised();
}

Expand Down Expand Up @@ -713,18 +713,18 @@ public function testInvalidDatesCombinedMaxPropertyPath($value, $dateTimeAsStrin
$constraint = new Range([
'minPropertyPath' => 'min',
'maxPropertyPath' => 'max',
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
'notInRangeMessage' => 'myNotInRangeMessage',
]);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMaxMessage')
$this->buildViolation('myNotInRangeMessage')
->setParameter('{{ value }}', $dateTimeAsString)
->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
->setParameter('{{ min }}', 'Mar 10, 2014, 12:00 AM')
->setParameter('{{ max }}', 'Mar 20, 2014, 12:00 AM')
->setParameter('{{ max_limit_path }}', 'max')
->setParameter('{{ min_limit_path }}', 'min')
->setCode(Range::TOO_HIGH_ERROR)
->setCode(Range::NOT_IN_RANGE_ERROR)
->assertRaised();
}

Expand All @@ -742,18 +742,18 @@ public function testInvalidDatesCombinedMinPropertyPath($value, $dateTimeAsStrin
$constraint = new Range([
'minPropertyPath' => 'min',
'maxPropertyPath' => 'max',
'minMessage' => 'myMinMessage',
'maxMessage' => 'myMaxMessage',
'notInRangeMessage' => 'myNotInRangeMessage',
]);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMinMessage')
$this->buildViolation('myNotInRangeMessage')
->setParameter('{{ value }}', $dateTimeAsString)
->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
->setParameter('{{ min }}', 'Mar 10, 2014, 12:00 AM')
->setParameter('{{ max }}', 'Mar 20, 2014, 12:00 AM')
->setParameter('{{ max_limit_path }}', 'max')
->setParameter('{{ min_limit_path }}', 'min')
->setCode(Range::TOO_LOW_ERROR)
->setCode(Range::NOT_IN_RANGE_ERROR)
->assertRaised();
}
}
Expand Down