From e9a8bbc0c9221e6802724bfccbb40ac02733da67 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 16 Jun 2025 16:37:14 +0200 Subject: [PATCH 1/3] Add min and max in both error message --- .../Validator/Constraints/LengthValidator.php | 16 ++++++++++++++-- .../Tests/Constraints/LengthValidatorTest.php | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/LengthValidator.php b/src/Symfony/Component/Validator/Constraints/LengthValidator.php index 985660bc2d777..b30f6722d5009 100644 --- a/src/Symfony/Component/Validator/Constraints/LengthValidator.php +++ b/src/Symfony/Component/Validator/Constraints/LengthValidator.php @@ -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) @@ -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) diff --git a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php index 10f61f50bf392..81351ffb2f3f7 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) From 4a539fb6afc2778c06ddf9c9b9a0e97f64345cd4 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 16 Jun 2025 17:13:04 +0200 Subject: [PATCH 2/3] Update tests --- .../Tests/Functional/ApiAttributesTest.php | 9 +++++++++ src/Symfony/Bundle/FrameworkBundle/composer.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php index 0dcfeaeba5ce2..8831fcd64acfe 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php @@ -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" @@ -439,6 +440,7 @@ public static function mapRequestPayloadProvider(): iterable "H" 10 + 10 1 urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45 @@ -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" @@ -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" @@ -680,6 +684,7 @@ public static function mapRequestPayloadProvider(): iterable "H" 10 + 10 1 urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45 @@ -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" @@ -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" @@ -926,6 +933,7 @@ public static function mapRequestPayloadProvider(): iterable "H" 10 + 10 1 urn:uuid:9ff3fdc4-b214-49db-8718-39c315e33d45 @@ -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" diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 4814cc601c84b..88213e7b3853c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -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", From 8df31976b8f5de99c5d0f4f4ba71611b8a641da9 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 16 Jun 2025 20:03:39 +0200 Subject: [PATCH 3/3] Update changelog --- src/Symfony/Component/Validator/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index e8146d2a50683..ddcc020d4b14f 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.4 +--- + + * Add the `min` and `max` parameter to the `Length` constraint violation + 7.3 --- 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