Skip to content

Commit 2663b3b

Browse files
committed
feature symfony#60805 [Validator] Add min and max in both error messages of LengthValidator (VincentLanglet)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [Validator] Add `min` and `max` in both error messages of `LengthValidator` | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | -- | License | MIT This would allow to use the same message for a Length constraint which say "The length needs to be between {{ min }} and {{ max }}". Commits ------- c465978 [Validator] Add `min` and `max` in both error messages of `LengthValidator`
2 parents c7493bb + c465978 commit 2663b3b

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ public static function mapRequestPayloadProvider(): iterable
405405
"parameters": {
406406
"{{ value }}": "\"\"",
407407
"{{ limit }}": "10",
408+
"{{ min }}": "10",
408409
"{{ value_length }}": "0"
409410
},
410411
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
@@ -439,6 +440,7 @@ public static function mapRequestPayloadProvider(): iterable
439440
<parameters>
440441
<item key="{{ value }}">"H"</item>
441442
<item key="{{ limit }}">10</item>
443+
<item key="{{ min }}">10</item>
442444
<item key="{{ value_length }}">1</item>
443445
</parameters>
444446
<type>urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45</type>
@@ -476,6 +478,7 @@ public static function mapRequestPayloadProvider(): iterable
476478
"parameters": {
477479
"{{ value }}": "\"\"",
478480
"{{ limit }}": "10",
481+
"{{ min }}": "10",
479482
"{{ value_length }}": "0"
480483
},
481484
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
@@ -646,6 +649,7 @@ public static function mapRequestPayloadProvider(): iterable
646649
"parameters": {
647650
"{{ value }}": "\"\"",
648651
"{{ limit }}": "10",
652+
"{{ min }}": "10",
649653
"{{ value_length }}": "0"
650654
},
651655
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
@@ -680,6 +684,7 @@ public static function mapRequestPayloadProvider(): iterable
680684
<parameters>
681685
<item key="{{ value }}">"H"</item>
682686
<item key="{{ limit }}">10</item>
687+
<item key="{{ min }}">10</item>
683688
<item key="{{ value_length }}">1</item>
684689
</parameters>
685690
<type>urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45</type>
@@ -717,6 +722,7 @@ public static function mapRequestPayloadProvider(): iterable
717722
"parameters": {
718723
"{{ value }}": "\"\"",
719724
"{{ limit }}": "10",
725+
"{{ min }}": "10",
720726
"{{ value_length }}": "0"
721727
},
722728
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
@@ -892,6 +898,7 @@ public static function mapRequestPayloadProvider(): iterable
892898
"parameters": {
893899
"{{ value }}": "\"\"",
894900
"{{ limit }}": "10",
901+
"{{ min }}": "10",
895902
"{{ value_length }}": "0"
896903
},
897904
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
@@ -926,6 +933,7 @@ public static function mapRequestPayloadProvider(): iterable
926933
<parameters>
927934
<item key="{{ value }}">"H"</item>
928935
<item key="{{ limit }}">10</item>
936+
<item key="{{ min }}">10</item>
929937
<item key="{{ value_length }}">1</item>
930938
</parameters>
931939
<type>urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45</type>
@@ -963,6 +971,7 @@ public static function mapRequestPayloadProvider(): iterable
963971
"parameters": {
964972
"{{ value }}": "\"\"",
965973
"{{ limit }}": "10",
974+
"{{ min }}": "10",
966975
"{{ value_length }}": "0"
967976
},
968977
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"symfony/translation": "^7.3|^8.0",
6868
"symfony/twig-bundle": "^6.4|^7.0|^8.0",
6969
"symfony/type-info": "^7.1.8|^8.0",
70-
"symfony/validator": "^6.4|^7.0|^8.0",
70+
"symfony/validator": "^7.4|^8.0",
7171
"symfony/workflow": "^7.3|^8.0",
7272
"symfony/yaml": "^6.4|^7.0|^8.0",
7373
"symfony/property-info": "^6.4|^7.0|^8.0",

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.4
55
---
66

7+
* Add the `min` and `max` parameter to the `Length` constraint violation
78
* Deprecate `getRequiredOptions()` and `getDefaultOption()` methods of the `All`, `AtLeastOneOf`, `CardScheme`, `Collection`,
89
`CssColor`, `Expression`, `Regex`, `Sequentially`, `Type`, and `When` constraints
910
* Deprecate evaluating options in the base `Constraint` class. Initialize properties in the constructor of the concrete constraint

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@ public function validate(mixed $value, Constraint $constraint): void
7171
if (null !== $constraint->max && $length > $constraint->max) {
7272
$exactlyOptionEnabled = $constraint->min == $constraint->max;
7373

74-
$this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->maxMessage)
74+
$builder = $this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->maxMessage);
75+
if (null !== $constraint->min) {
76+
$builder->setParameter('{{ min }}', $constraint->min);
77+
}
78+
79+
$builder
7580
->setParameter('{{ value }}', $this->formatValue($stringValue))
7681
->setParameter('{{ limit }}', $constraint->max)
82+
->setParameter('{{ max }}', $constraint->max) // To be consistent with the min error message
7783
->setParameter('{{ value_length }}', $length)
7884
->setInvalidValue($value)
7985
->setPlural($constraint->max)
@@ -86,9 +92,15 @@ public function validate(mixed $value, Constraint $constraint): void
8692
if (null !== $constraint->min && $length < $constraint->min) {
8793
$exactlyOptionEnabled = $constraint->min == $constraint->max;
8894

89-
$this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->minMessage)
95+
$builder = $this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->minMessage);
96+
if (null !== $constraint->max) {
97+
$builder->setParameter('{{ max }}', $constraint->max);
98+
}
99+
100+
$builder
90101
->setParameter('{{ value }}', $this->formatValue($stringValue))
91102
->setParameter('{{ limit }}', $constraint->min)
103+
->setParameter('{{ min }}', $constraint->min) // To be consistent with the max error message
92104
->setParameter('{{ value_length }}', $length)
93105
->setInvalidValue($value)
94106
->setPlural($constraint->min)

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function testEmptyStringIsInvalid()
4040
$this->buildViolation('myMessage')
4141
->setParameter('{{ value }}', '""')
4242
->setParameter('{{ limit }}', $limit)
43+
->setParameter('{{ min }}', $limit)
44+
->setParameter('{{ max }}', $limit)
4345
->setParameter('{{ value_length }}', 0)
4446
->setInvalidValue('')
4547
->setPlural($limit)
@@ -196,6 +198,7 @@ public function testInvalidValuesMin(int|string $value, int $valueLength)
196198
$this->buildViolation('myMessage')
197199
->setParameter('{{ value }}', '"'.$value.'"')
198200
->setParameter('{{ limit }}', 4)
201+
->setParameter('{{ min }}', 4)
199202
->setParameter('{{ value_length }}', $valueLength)
200203
->setInvalidValue($value)
201204
->setPlural(4)
@@ -215,6 +218,7 @@ public function testInvalidValuesMinNamed(int|string $value, int $valueLength)
215218
$this->buildViolation('myMessage')
216219
->setParameter('{{ value }}', '"'.$value.'"')
217220
->setParameter('{{ limit }}', 4)
221+
->setParameter('{{ min }}', 4)
218222
->setParameter('{{ value_length }}', $valueLength)
219223
->setInvalidValue($value)
220224
->setPlural(4)
@@ -237,6 +241,7 @@ public function testInvalidValuesMax(int|string $value, int $valueLength)
237241
$this->buildViolation('myMessage')
238242
->setParameter('{{ value }}', '"'.$value.'"')
239243
->setParameter('{{ limit }}', 4)
244+
->setParameter('{{ max }}', 4)
240245
->setParameter('{{ value_length }}', $valueLength)
241246
->setInvalidValue($value)
242247
->setPlural(4)
@@ -256,6 +261,7 @@ public function testInvalidValuesMaxNamed(int|string $value, int $valueLength)
256261
$this->buildViolation('myMessage')
257262
->setParameter('{{ value }}', '"'.$value.'"')
258263
->setParameter('{{ limit }}', 4)
264+
->setParameter('{{ max }}', 4)
259265
->setParameter('{{ value_length }}', $valueLength)
260266
->setInvalidValue($value)
261267
->setPlural(4)
@@ -279,6 +285,8 @@ public function testInvalidValuesExactLessThanFour(int|string $value, int $value
279285
$this->buildViolation('myMessage')
280286
->setParameter('{{ value }}', '"'.$value.'"')
281287
->setParameter('{{ limit }}', 4)
288+
->setParameter('{{ min }}', 4)
289+
->setParameter('{{ max }}', 4)
282290
->setParameter('{{ value_length }}', $valueLength)
283291
->setInvalidValue($value)
284292
->setPlural(4)
@@ -298,6 +306,8 @@ public function testInvalidValuesExactLessThanFourNamed(int|string $value, int $
298306
$this->buildViolation('myMessage')
299307
->setParameter('{{ value }}', '"'.$value.'"')
300308
->setParameter('{{ limit }}', 4)
309+
->setParameter('{{ min }}', 4)
310+
->setParameter('{{ max }}', 4)
301311
->setParameter('{{ value_length }}', $valueLength)
302312
->setInvalidValue($value)
303313
->setPlural(4)
@@ -321,6 +331,8 @@ public function testInvalidValuesExactMoreThanFour(int|string $value, int $value
321331
$this->buildViolation('myMessage')
322332
->setParameter('{{ value }}', '"'.$value.'"')
323333
->setParameter('{{ limit }}', 4)
334+
->setParameter('{{ min }}', 4)
335+
->setParameter('{{ max }}', 4)
324336
->setParameter('{{ value_length }}', $valueLength)
325337
->setInvalidValue($value)
326338
->setPlural(4)
@@ -363,6 +375,8 @@ public function testInvalidValuesExactDefaultCountUnitWithGraphemeInput()
363375
$this->buildViolation('myMessage')
364376
->setParameter('{{ value }}', '"'."A\u{0300}".'"')
365377
->setParameter('{{ limit }}', 1)
378+
->setParameter('{{ min }}', 1)
379+
->setParameter('{{ max }}', 1)
366380
->setParameter('{{ value_length }}', 2)
367381
->setInvalidValue("A\u{0300}")
368382
->setPlural(1)
@@ -379,6 +393,8 @@ public function testInvalidValuesExactBytesCountUnitWithGraphemeInput()
379393
$this->buildViolation('myMessage')
380394
->setParameter('{{ value }}', '"'."A\u{0300}".'"')
381395
->setParameter('{{ limit }}', 1)
396+
->setParameter('{{ min }}', 1)
397+
->setParameter('{{ max }}', 1)
382398
->setParameter('{{ value_length }}', 3)
383399
->setInvalidValue("A\u{0300}")
384400
->setPlural(1)

0 commit comments

Comments
 (0)