Skip to content

[Validator] Add min and max in both error messages of LengthValidator #60805

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ public static function mapRequestPayloadProvider(): iterable
"parameters": {
"{{ value }}": "\"\"",
"{{ limit }}": "10",
"{{ min }}": "10",
"{{ value_length }}": "0"
},
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
Expand Down Expand Up @@ -439,6 +440,7 @@ public static function mapRequestPayloadProvider(): iterable
<parameters>
<item key="{{ value }}">"H"</item>
<item key="{{ limit }}">10</item>
<item key="{{ min }}">10</item>
<item key="{{ value_length }}">1</item>
</parameters>
<type>urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45</type>
Expand Down Expand Up @@ -476,6 +478,7 @@ public static function mapRequestPayloadProvider(): iterable
"parameters": {
"{{ value }}": "\"\"",
"{{ limit }}": "10",
"{{ min }}": "10",
"{{ value_length }}": "0"
},
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
Expand Down Expand Up @@ -646,6 +649,7 @@ public static function mapRequestPayloadProvider(): iterable
"parameters": {
"{{ value }}": "\"\"",
"{{ limit }}": "10",
"{{ min }}": "10",
"{{ value_length }}": "0"
},
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
Expand Down Expand Up @@ -680,6 +684,7 @@ public static function mapRequestPayloadProvider(): iterable
<parameters>
<item key="{{ value }}">"H"</item>
<item key="{{ limit }}">10</item>
<item key="{{ min }}">10</item>
<item key="{{ value_length }}">1</item>
</parameters>
<type>urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45</type>
Expand Down Expand Up @@ -717,6 +722,7 @@ public static function mapRequestPayloadProvider(): iterable
"parameters": {
"{{ value }}": "\"\"",
"{{ limit }}": "10",
"{{ min }}": "10",
"{{ value_length }}": "0"
},
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
Expand Down Expand Up @@ -892,6 +898,7 @@ public static function mapRequestPayloadProvider(): iterable
"parameters": {
"{{ value }}": "\"\"",
"{{ limit }}": "10",
"{{ min }}": "10",
"{{ value_length }}": "0"
},
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
Expand Down Expand Up @@ -926,6 +933,7 @@ public static function mapRequestPayloadProvider(): iterable
<parameters>
<item key="{{ value }}">"H"</item>
<item key="{{ limit }}">10</item>
<item key="{{ min }}">10</item>
<item key="{{ value_length }}">1</item>
</parameters>
<type>urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45</type>
Expand Down Expand Up @@ -963,6 +971,7 @@ public static function mapRequestPayloadProvider(): iterable
"parameters": {
"{{ value }}": "\"\"",
"{{ limit }}": "10",
"{{ min }}": "10",
"{{ value_length }}": "0"
},
"type": "urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45"
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"symfony/translation": "^7.3|^8.0",
"symfony/twig-bundle": "^6.4|^7.0|^8.0",
"symfony/type-info": "^7.1|^8.0",
"symfony/validator": "^6.4|^7.0|^8.0",
"symfony/validator": "^7.4|^8.0",
"symfony/workflow": "^7.3|^8.0",
"symfony/yaml": "^6.4|^7.0|^8.0",
"symfony/property-info": "^6.4|^7.0|^8.0",
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add the `min` and `max` parameter to the `Length` constraint violation

7.3
---

Expand Down
16 changes: 14 additions & 2 deletions src/Symfony/Component/Validator/Constraints/LengthValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ public function validate(mixed $value, Constraint $constraint): void
if (null !== $constraint->max && $length > $constraint->max) {
$exactlyOptionEnabled = $constraint->min == $constraint->max;

$this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->maxMessage)
$builder = $this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->maxMessage);
if (null !== $constraint->min) {
$builder->setParameter('{{ min }}', $constraint->min);
}

$builder
->setParameter('{{ value }}', $this->formatValue($stringValue))
->setParameter('{{ limit }}', $constraint->max)
->setParameter('{{ max }}', $constraint->max) // To be consistent with the min error message
->setParameter('{{ value_length }}', $length)
->setInvalidValue($value)
->setPlural($constraint->max)
Expand All @@ -86,9 +92,15 @@ public function validate(mixed $value, Constraint $constraint): void
if (null !== $constraint->min && $length < $constraint->min) {
$exactlyOptionEnabled = $constraint->min == $constraint->max;

$this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->minMessage)
$builder = $this->context->buildViolation($exactlyOptionEnabled ? $constraint->exactMessage : $constraint->minMessage);
if (null !== $constraint->max) {
$builder->setParameter('{{ max }}', $constraint->max);
}

$builder
->setParameter('{{ value }}', $this->formatValue($stringValue))
->setParameter('{{ limit }}', $constraint->min)
->setParameter('{{ min }}', $constraint->min) // To be consistent with the max error message
->setParameter('{{ value_length }}', $length)
->setInvalidValue($value)
->setPlural($constraint->min)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function testEmptyStringIsInvalid()
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '""')
->setParameter('{{ limit }}', $limit)
->setParameter('{{ min }}', $limit)
->setParameter('{{ max }}', $limit)
->setParameter('{{ value_length }}', 0)
->setInvalidValue('')
->setPlural($limit)
Expand Down Expand Up @@ -196,6 +198,7 @@ public function testInvalidValuesMin(int|string $value, int $valueLength)
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ limit }}', 4)
->setParameter('{{ min }}', 4)
->setParameter('{{ value_length }}', $valueLength)
->setInvalidValue($value)
->setPlural(4)
Expand All @@ -215,6 +218,7 @@ public function testInvalidValuesMinNamed(int|string $value, int $valueLength)
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ limit }}', 4)
->setParameter('{{ min }}', 4)
->setParameter('{{ value_length }}', $valueLength)
->setInvalidValue($value)
->setPlural(4)
Expand All @@ -237,6 +241,7 @@ public function testInvalidValuesMax(int|string $value, int $valueLength)
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ limit }}', 4)
->setParameter('{{ max }}', 4)
->setParameter('{{ value_length }}', $valueLength)
->setInvalidValue($value)
->setPlural(4)
Expand All @@ -256,6 +261,7 @@ public function testInvalidValuesMaxNamed(int|string $value, int $valueLength)
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ limit }}', 4)
->setParameter('{{ max }}', 4)
->setParameter('{{ value_length }}', $valueLength)
->setInvalidValue($value)
->setPlural(4)
Expand All @@ -279,6 +285,8 @@ public function testInvalidValuesExactLessThanFour(int|string $value, int $value
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ limit }}', 4)
->setParameter('{{ min }}', 4)
->setParameter('{{ max }}', 4)
->setParameter('{{ value_length }}', $valueLength)
->setInvalidValue($value)
->setPlural(4)
Expand All @@ -298,6 +306,8 @@ public function testInvalidValuesExactLessThanFourNamed(int|string $value, int $
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ limit }}', 4)
->setParameter('{{ min }}', 4)
->setParameter('{{ max }}', 4)
->setParameter('{{ value_length }}', $valueLength)
->setInvalidValue($value)
->setPlural(4)
Expand All @@ -321,6 +331,8 @@ public function testInvalidValuesExactMoreThanFour(int|string $value, int $value
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ limit }}', 4)
->setParameter('{{ min }}', 4)
->setParameter('{{ max }}', 4)
->setParameter('{{ value_length }}', $valueLength)
->setInvalidValue($value)
->setPlural(4)
Expand Down Expand Up @@ -363,6 +375,8 @@ public function testInvalidValuesExactDefaultCountUnitWithGraphemeInput()
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'."A\u{0300}".'"')
->setParameter('{{ limit }}', 1)
->setParameter('{{ min }}', 1)
->setParameter('{{ max }}', 1)
->setParameter('{{ value_length }}', 2)
->setInvalidValue("A\u{0300}")
->setPlural(1)
Expand All @@ -379,6 +393,8 @@ public function testInvalidValuesExactBytesCountUnitWithGraphemeInput()
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'."A\u{0300}".'"')
->setParameter('{{ limit }}', 1)
->setParameter('{{ min }}', 1)
->setParameter('{{ max }}', 1)
->setParameter('{{ value_length }}', 3)
->setInvalidValue("A\u{0300}")
->setPlural(1)
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy