Skip to content

Commit 50faef9

Browse files
committed
bug symfony#54750 [Validator] detect wrong usages of minMessage/maxMessage in options (xabbuh)
This PR was merged into the 6.4 branch. Discussion ---------- [Validator] detect wrong usages of minMessage/maxMessage in options | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Commits ------- 600880e detect wrong usages of minMessage/maxMessage in options
2 parents b7eb42e + 600880e commit 50faef9

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function __construct(
9595
throw new LogicException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "minPropertyPath" or "maxPropertyPath" option. Try running "composer require symfony/property-access".', static::class));
9696
}
9797

98-
if (null !== $this->min && null !== $this->max && ($minMessage || $maxMessage)) {
98+
if (null !== $this->min && null !== $this->max && ($minMessage || $maxMessage || isset($options['minMessage']) || isset($options['maxMessage']))) {
9999
throw new ConstraintDefinitionException(sprintf('The "%s" constraint can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.', static::class));
100100
}
101101
}

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testThrowsConstraintExceptionIfBothMinLimitAndPropertyPath()
3030

3131
public function testThrowsConstraintExceptionIfBothMinLimitAndPropertyPathNamed()
3232
{
33-
$this->expectException(\Symfony\Component\Validator\Exception\ConstraintDefinitionException::class);
33+
$this->expectException(ConstraintDefinitionException::class);
3434
$this->expectExceptionMessage('requires only one of the "min" or "minPropertyPath" options to be set, not both.');
3535
new Range(min: 'min', minPropertyPath: 'minPropertyPath');
3636
}
@@ -47,7 +47,7 @@ public function testThrowsConstraintExceptionIfBothMaxLimitAndPropertyPath()
4747

4848
public function testThrowsConstraintExceptionIfBothMaxLimitAndPropertyPathNamed()
4949
{
50-
$this->expectException(\Symfony\Component\Validator\Exception\ConstraintDefinitionException::class);
50+
$this->expectException(ConstraintDefinitionException::class);
5151
$this->expectExceptionMessage('requires only one of the "max" or "maxPropertyPath" options to be set, not both.');
5252
new Range(max: 'max', maxPropertyPath: 'maxPropertyPath');
5353
}
@@ -65,10 +65,58 @@ public function testThrowsNoDefaultOptionConfiguredException()
6565
new Range('value');
6666
}
6767

68-
public function testThrowsConstraintDefinitionExceptionIfBothMinAndMaxAndMinMessageOrMaxMessage()
68+
public function testThrowsConstraintDefinitionExceptionIfBothMinAndMaxAndMinMessageAndMaxMessage()
6969
{
70-
$this->expectException(\Symfony\Component\Validator\Exception\ConstraintDefinitionException::class);
70+
$this->expectException(ConstraintDefinitionException::class);
7171
$this->expectExceptionMessage('can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.');
7272
new Range(min: 'min', max: 'max', minMessage: 'minMessage', maxMessage: 'maxMessage');
7373
}
74+
75+
public function testThrowsConstraintDefinitionExceptionIfBothMinAndMaxAndMinMessage()
76+
{
77+
$this->expectException(ConstraintDefinitionException::class);
78+
$this->expectExceptionMessage('can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.');
79+
new Range(min: 'min', max: 'max', minMessage: 'minMessage');
80+
}
81+
82+
public function testThrowsConstraintDefinitionExceptionIfBothMinAndMaxAndMaxMessage()
83+
{
84+
$this->expectException(ConstraintDefinitionException::class);
85+
$this->expectExceptionMessage('can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.');
86+
new Range(min: 'min', max: 'max', maxMessage: 'maxMessage');
87+
}
88+
89+
public function testThrowsConstraintDefinitionExceptionIfBothMinAndMaxAndMinMessageAndMaxMessageOptions()
90+
{
91+
$this->expectException(ConstraintDefinitionException::class);
92+
$this->expectExceptionMessage('can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.');
93+
new Range([
94+
'min' => 'min',
95+
'minMessage' => 'minMessage',
96+
'max' => 'max',
97+
'maxMessage' => 'maxMessage',
98+
]);
99+
}
100+
101+
public function testThrowsConstraintDefinitionExceptionIfBothMinAndMaxAndMinMessageOptions()
102+
{
103+
$this->expectException(ConstraintDefinitionException::class);
104+
$this->expectExceptionMessage('can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.');
105+
new Range([
106+
'min' => 'min',
107+
'minMessage' => 'minMessage',
108+
'max' => 'max',
109+
]);
110+
}
111+
112+
public function testThrowsConstraintDefinitionExceptionIfBothMinAndMaxAndMaxMessageOptions()
113+
{
114+
$this->expectException(ConstraintDefinitionException::class);
115+
$this->expectExceptionMessage('can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.');
116+
new Range([
117+
'min' => 'min',
118+
'max' => 'max',
119+
'maxMessage' => 'maxMessage',
120+
]);
121+
}
74122
}

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,30 +1037,6 @@ public static function provideMessageIfMinAndMaxSet(): array
10371037
'not_in_range_message',
10381038
Range::NOT_IN_RANGE_ERROR,
10391039
],
1040-
[
1041-
['minMessage' => 'min_message'],
1042-
0,
1043-
$notInRangeMessage,
1044-
Range::NOT_IN_RANGE_ERROR,
1045-
],
1046-
[
1047-
['maxMessage' => 'max_message'],
1048-
0,
1049-
$notInRangeMessage,
1050-
Range::NOT_IN_RANGE_ERROR,
1051-
],
1052-
[
1053-
['minMessage' => 'min_message'],
1054-
15,
1055-
$notInRangeMessage,
1056-
Range::NOT_IN_RANGE_ERROR,
1057-
],
1058-
[
1059-
['maxMessage' => 'max_message'],
1060-
15,
1061-
$notInRangeMessage,
1062-
Range::NOT_IN_RANGE_ERROR,
1063-
],
10641040
];
10651041
}
10661042

0 commit comments

Comments
 (0)