Skip to content

Commit 0b018cc

Browse files
committed
minor symfony#57755 [Validator] Fix constraints on WordCount properties (alexandre-daubois)
This PR was merged into the 7.2 branch. Discussion ---------- [Validator] Fix constraints on `WordCount` properties | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT On second thought, it doesn't make sense to allow `min` to be set at 0 in `WordCount`. Minimum word count allowed should be 1 for the constraint to have a meaning, or `null` if no minimum is set. Commits ------- 602d84f [Validator] Fix constraints on `WordCount` properties
2 parents e63495e + 602d84f commit 0b018cc

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function __construct(
4848
throw new MissingOptionsException(\sprintf('Either option "min" or "max" must be given for constraint "%s".', __CLASS__), ['min', 'max']);
4949
}
5050

51-
if (null !== $min && $min < 0) {
52-
throw new ConstraintDefinitionException(\sprintf('The "%s" constraint requires the min word count to be a positive integer or 0 if set.', __CLASS__));
51+
if (null !== $min && $min <= 0) {
52+
throw new ConstraintDefinitionException(\sprintf('The "%s" constraint requires the min word count to be a positive integer if set.', __CLASS__));
5353
}
5454

5555
if (null !== $max && $max <= 0) {

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,31 @@ public function testOnlyMaxIsSet()
4848
$this->assertNull($wordCount->locale);
4949
}
5050

51-
public function testMinMustBeNatural()
51+
public function testMinIsNegative()
5252
{
5353
$this->expectException(ConstraintDefinitionException::class);
54-
$this->expectExceptionMessage('The "Symfony\Component\Validator\Constraints\WordCount" constraint requires the min word count to be a positive integer or 0 if set.');
54+
$this->expectExceptionMessage('The "Symfony\Component\Validator\Constraints\WordCount" constraint requires the min word count to be a positive integer if set.');
5555

5656
new WordCount(-1);
5757
}
5858

59-
public function testMaxMustBePositive()
59+
public function testMinIsZero()
60+
{
61+
$this->expectException(ConstraintDefinitionException::class);
62+
$this->expectExceptionMessage('The "Symfony\Component\Validator\Constraints\WordCount" constraint requires the min word count to be a positive integer if set.');
63+
64+
new WordCount(0);
65+
}
66+
67+
public function testMaxIsNegative()
68+
{
69+
$this->expectException(ConstraintDefinitionException::class);
70+
$this->expectExceptionMessage('The "Symfony\Component\Validator\Constraints\WordCount" constraint requires the max word count to be a positive integer if set.');
71+
72+
new WordCount(max: -1);
73+
}
74+
75+
public function testMaxIsZero()
6076
{
6177
$this->expectException(ConstraintDefinitionException::class);
6278
$this->expectExceptionMessage('The "Symfony\Component\Validator\Constraints\WordCount" constraint requires the max word count to be a positive integer if set.');

0 commit comments

Comments
 (0)