Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5488bc

Browse files
Lctrsfabpot
authored andcommitted
[Validator] Add a new constraint message when there is both min and max
1 parent f1dff5e commit c5488bc

File tree

6 files changed

+78
-42
lines changed

6 files changed

+78
-42
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ CHANGELOG
1818
* added the `limit_path` parameter in violations when using
1919
`Range` constraint with the `minPropertyPath` or
2020
`maxPropertyPath` options.
21+
* added a new `notInRangeMessage` options to the `Range` constraint that will
22+
be used in the violation builder when both `min` and `max` are not null.
2123

2224
4.3.0
2325
-----

src/Symfony/Component/Validator/Constraints/Range.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@
2626
class Range extends Constraint
2727
{
2828
const INVALID_CHARACTERS_ERROR = 'ad9a9798-7a99-4df7-8ce9-46e416a1e60b';
29+
const NOT_IN_RANGE_ERROR = '04b91c99-a946-4221-afc5-e65ebac401eb';
2930
const TOO_HIGH_ERROR = '2d28afcb-e32e-45fb-a815-01c431a86a69';
3031
const TOO_LOW_ERROR = '76454e69-502c-46c5-9643-f447d837c4d5';
3132

3233
protected static $errorNames = [
3334
self::INVALID_CHARACTERS_ERROR => 'INVALID_CHARACTERS_ERROR',
35+
self::NOT_IN_RANGE_ERROR => 'NOT_IN_RANGE_ERROR',
3436
self::TOO_HIGH_ERROR => 'TOO_HIGH_ERROR',
3537
self::TOO_LOW_ERROR => 'TOO_LOW_ERROR',
3638
];
3739

40+
public $notInRangeMessage = 'This value should be between {{ min }} and {{ max }}.';
3841
public $minMessage = 'This value should be {{ limit }} or more.';
3942
public $maxMessage = 'This value should be {{ limit }} or less.';
4043
public $invalidMessage = 'This value should be a valid number.';

src/Symfony/Component/Validator/Constraints/RangeValidator.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,30 @@ public function validate($value, Constraint $constraint)
7070
}
7171
}
7272

73-
if (null !== $max && $value > $max) {
73+
$hasLowerLimit = null !== $min;
74+
$hasUpperLimit = null !== $max;
75+
76+
if ($hasLowerLimit && $hasUpperLimit && ($value < $min || $value > $max)) {
77+
$violationBuilder = $this->context->buildViolation($constraint->notInRangeMessage)
78+
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
79+
->setParameter('{{ min }}', $this->formatValue($min, self::PRETTY_DATE))
80+
->setParameter('{{ max }}', $this->formatValue($max, self::PRETTY_DATE))
81+
->setCode(Range::NOT_IN_RANGE_ERROR);
82+
83+
if (null !== $constraint->maxPropertyPath) {
84+
$violationBuilder->setParameter('{{ max_limit_path }}', $constraint->maxPropertyPath);
85+
}
86+
87+
if (null !== $constraint->minPropertyPath) {
88+
$violationBuilder->setParameter('{{ min_limit_path }}', $constraint->minPropertyPath);
89+
}
90+
91+
$violationBuilder->addViolation();
92+
93+
return;
94+
}
95+
96+
if ($hasUpperLimit && $value > $max) {
7497
$violationBuilder = $this->context->buildViolation($constraint->maxMessage)
7598
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
7699
->setParameter('{{ limit }}', $this->formatValue($max, self::PRETTY_DATE))
@@ -89,7 +112,7 @@ public function validate($value, Constraint $constraint)
89112
return;
90113
}
91114

92-
if (null !== $min && $value < $min) {
115+
if ($hasLowerLimit && $value < $min) {
93116
$violationBuilder = $this->context->buildViolation($constraint->minMessage)
94117
->setParameter('{{ value }}', $this->formatValue($value, self::PRETTY_DATE))
95118
->setParameter('{{ limit }}', $this->formatValue($min, self::PRETTY_DATE))

src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@
362362
<source>This password has been leaked in a data breach, it must not be used. Please use another password.</source>
363363
<target>This password has been leaked in a data breach, it must not be used. Please use another password.</target>
364364
</trans-unit>
365+
<trans-unit id="94">
366+
<source>This value should be between {{ min }} and {{ max }}.</source>
367+
<target>This value should be between {{ min }} and {{ max }}.</target>
368+
</trans-unit>
365369
</body>
366370
</file>
367371
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@
362362
<source>This password has been leaked in a data breach, it must not be used. Please use another password.</source>
363363
<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>
364364
</trans-unit>
365+
<trans-unit id="94">
366+
<source>This value should be between {{ min }} and {{ max }}.</source>
367+
<target>Cette valeur doit être comprise entre {{ min }} et {{ max }}.</target>
368+
</trans-unit>
365369
</body>
366370
</file>
367371
</xliff>

src/Symfony/Component/Validator/Tests/Constraints/RangeValidatorTest.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,16 @@ public function testInvalidValuesCombinedMax($value, $formattedValue)
143143
$constraint = new Range([
144144
'min' => 10,
145145
'max' => 20,
146-
'minMessage' => 'myMinMessage',
147-
'maxMessage' => 'myMaxMessage',
146+
'notInRangeMessage' => 'myNotInRangeMessage',
148147
]);
149148

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

152-
$this->buildViolation('myMaxMessage')
151+
$this->buildViolation('myNotInRangeMessage')
153152
->setParameter('{{ value }}', $formattedValue)
154-
->setParameter('{{ limit }}', 20)
155-
->setCode(Range::TOO_HIGH_ERROR)
153+
->setParameter('{{ min }}', 10)
154+
->setParameter('{{ max }}', 20)
155+
->setCode(Range::NOT_IN_RANGE_ERROR)
156156
->assertRaised();
157157
}
158158

@@ -164,16 +164,16 @@ public function testInvalidValuesCombinedMin($value, $formattedValue)
164164
$constraint = new Range([
165165
'min' => 10,
166166
'max' => 20,
167-
'minMessage' => 'myMinMessage',
168-
'maxMessage' => 'myMaxMessage',
167+
'notInRangeMessage' => 'myNotInRangeMessage',
169168
]);
170169

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

173-
$this->buildViolation('myMinMessage')
172+
$this->buildViolation('myNotInRangeMessage')
174173
->setParameter('{{ value }}', $formattedValue)
175-
->setParameter('{{ limit }}', 10)
176-
->setCode(Range::TOO_LOW_ERROR)
174+
->setParameter('{{ min }}', 10)
175+
->setParameter('{{ max }}', 20)
176+
->setCode(Range::NOT_IN_RANGE_ERROR)
177177
->assertRaised();
178178
}
179179

@@ -327,16 +327,16 @@ public function testInvalidDatesCombinedMax($value, $dateTimeAsString)
327327
$constraint = new Range([
328328
'min' => 'March 10, 2014',
329329
'max' => 'March 20, 2014',
330-
'minMessage' => 'myMinMessage',
331-
'maxMessage' => 'myMaxMessage',
330+
'notInRangeMessage' => 'myNotInRangeMessage',
332331
]);
333332

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

336-
$this->buildViolation('myMaxMessage')
335+
$this->buildViolation('myNotInRangeMessage')
337336
->setParameter('{{ value }}', $dateTimeAsString)
338-
->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
339-
->setCode(Range::TOO_HIGH_ERROR)
337+
->setParameter('{{ min }}', 'Mar 10, 2014, 12:00 AM')
338+
->setParameter('{{ max }}', 'Mar 20, 2014, 12:00 AM')
339+
->setCode(Range::NOT_IN_RANGE_ERROR)
340340
->assertRaised();
341341
}
342342

@@ -352,16 +352,16 @@ public function testInvalidDatesCombinedMin($value, $dateTimeAsString)
352352
$constraint = new Range([
353353
'min' => 'March 10, 2014',
354354
'max' => 'March 20, 2014',
355-
'minMessage' => 'myMinMessage',
356-
'maxMessage' => 'myMaxMessage',
355+
'notInRangeMessage' => 'myNotInRangeMessage',
357356
]);
358357

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

361-
$this->buildViolation('myMinMessage')
360+
$this->buildViolation('myNotInRangeMessage')
362361
->setParameter('{{ value }}', $dateTimeAsString)
363-
->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
364-
->setCode(Range::TOO_LOW_ERROR)
362+
->setParameter('{{ min }}', 'Mar 10, 2014, 12:00 AM')
363+
->setParameter('{{ max }}', 'Mar 20, 2014, 12:00 AM')
364+
->setCode(Range::NOT_IN_RANGE_ERROR)
365365
->assertRaised();
366366
}
367367

@@ -527,18 +527,18 @@ public function testInvalidValuesCombinedMaxPropertyPath($value, $formattedValue
527527
$constraint = new Range([
528528
'minPropertyPath' => 'min',
529529
'maxPropertyPath' => 'max',
530-
'minMessage' => 'myMinMessage',
531-
'maxMessage' => 'myMaxMessage',
530+
'notInRangeMessage' => 'myNotInRangeMessage',
532531
]);
533532

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

536-
$this->buildViolation('myMaxMessage')
535+
$this->buildViolation('myNotInRangeMessage')
537536
->setParameter('{{ value }}', $formattedValue)
538-
->setParameter('{{ limit }}', 20)
537+
->setParameter('{{ min }}', 10)
538+
->setParameter('{{ max }}', 20)
539539
->setParameter('{{ max_limit_path }}', 'max')
540540
->setParameter('{{ min_limit_path }}', 'min')
541-
->setCode(Range::TOO_HIGH_ERROR)
541+
->setCode(Range::NOT_IN_RANGE_ERROR)
542542
->assertRaised();
543543
}
544544

@@ -552,18 +552,18 @@ public function testInvalidValuesCombinedMinPropertyPath($value, $formattedValue
552552
$constraint = new Range([
553553
'minPropertyPath' => 'min',
554554
'maxPropertyPath' => 'max',
555-
'minMessage' => 'myMinMessage',
556-
'maxMessage' => 'myMaxMessage',
555+
'notInRangeMessage' => 'myNotInRangeMessage',
557556
]);
558557

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

561-
$this->buildViolation('myMinMessage')
560+
$this->buildViolation('myNotInRangeMessage')
562561
->setParameter('{{ value }}', $formattedValue)
563-
->setParameter('{{ limit }}', 10)
562+
->setParameter('{{ min }}', 10)
563+
->setParameter('{{ max }}', 20)
564564
->setParameter('{{ max_limit_path }}', 'max')
565565
->setParameter('{{ min_limit_path }}', 'min')
566-
->setCode(Range::TOO_LOW_ERROR)
566+
->setCode(Range::NOT_IN_RANGE_ERROR)
567567
->assertRaised();
568568
}
569569

@@ -713,18 +713,18 @@ public function testInvalidDatesCombinedMaxPropertyPath($value, $dateTimeAsStrin
713713
$constraint = new Range([
714714
'minPropertyPath' => 'min',
715715
'maxPropertyPath' => 'max',
716-
'minMessage' => 'myMinMessage',
717-
'maxMessage' => 'myMaxMessage',
716+
'notInRangeMessage' => 'myNotInRangeMessage',
718717
]);
719718

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

722-
$this->buildViolation('myMaxMessage')
721+
$this->buildViolation('myNotInRangeMessage')
723722
->setParameter('{{ value }}', $dateTimeAsString)
724-
->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
723+
->setParameter('{{ min }}', 'Mar 10, 2014, 12:00 AM')
724+
->setParameter('{{ max }}', 'Mar 20, 2014, 12:00 AM')
725725
->setParameter('{{ max_limit_path }}', 'max')
726726
->setParameter('{{ min_limit_path }}', 'min')
727-
->setCode(Range::TOO_HIGH_ERROR)
727+
->setCode(Range::NOT_IN_RANGE_ERROR)
728728
->assertRaised();
729729
}
730730

@@ -742,18 +742,18 @@ public function testInvalidDatesCombinedMinPropertyPath($value, $dateTimeAsStrin
742742
$constraint = new Range([
743743
'minPropertyPath' => 'min',
744744
'maxPropertyPath' => 'max',
745-
'minMessage' => 'myMinMessage',
746-
'maxMessage' => 'myMaxMessage',
745+
'notInRangeMessage' => 'myNotInRangeMessage',
747746
]);
748747

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

751-
$this->buildViolation('myMinMessage')
750+
$this->buildViolation('myNotInRangeMessage')
752751
->setParameter('{{ value }}', $dateTimeAsString)
753-
->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
752+
->setParameter('{{ min }}', 'Mar 10, 2014, 12:00 AM')
753+
->setParameter('{{ max }}', 'Mar 20, 2014, 12:00 AM')
754754
->setParameter('{{ max_limit_path }}', 'max')
755755
->setParameter('{{ min_limit_path }}', 'min')
756-
->setCode(Range::TOO_LOW_ERROR)
756+
->setCode(Range::NOT_IN_RANGE_ERROR)
757757
->assertRaised();
758758
}
759759
}

0 commit comments

Comments
 (0)