Skip to content
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
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 @@ -67,7 +67,7 @@
"symfony/translation": "^7.3|^8.0",
"symfony/twig-bundle": "^6.4|^7.0|^8.0",
"symfony/type-info": "^7.1.8|^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
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.4
---

* Add the `min` and `max` parameter to the `Length` constraint violation
* Deprecate `getRequiredOptions()` and `getDefaultOption()` methods of the `All`, `AtLeastOneOf`, `CardScheme`, `Collection`,
`CssColor`, `Expression`, `Regex`, `Sequentially`, `Type`, and `When` constraints
* Deprecate evaluating options in the base `Constraint` class. Initialize properties in the constructor of the concrete constraint
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
Loading