Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07f30ec

Browse files
committed
[Validator] Allow empty string for LengthValidator
Allow empty string to match documentation "As with most of the other constraints, null and empty strings are considered valid values".
1 parent 7e13694 commit 07f30ec

File tree

3 files changed

+5
-15
lines changed

3 files changed

+5
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function validate(mixed $value, Constraint $constraint)
3030
throw new UnexpectedTypeException($constraint, Length::class);
3131
}
3232

33-
if (null === $value) {
33+
if (null === $value || '' === $value) {
3434
return;
3535
}
3636

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,11 @@ public function testNullIsValid()
3030
$this->assertNoViolation();
3131
}
3232

33-
public function testEmptyStringIsInvalid()
33+
public function testEmptyStringIsValid()
3434
{
35-
$this->validator->validate('', new Length([
36-
'value' => $limit = 6,
37-
'exactMessage' => 'myMessage',
38-
]));
35+
$this->validator->validate('', new Length(['value' => 6]));
3936

40-
$this->buildViolation('myMessage')
41-
->setParameter('{{ value }}', '""')
42-
->setParameter('{{ limit }}', $limit)
43-
->setInvalidValue('')
44-
->setPlural($limit)
45-
->setCode(Length::NOT_EQUAL_LENGTH_ERROR)
46-
->assertRaised();
37+
$this->assertNoViolation();
4738
}
4839

4940
public function testExpectsStringCompatibleType()

src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,10 +2202,9 @@ public function testAllConstraintValidateAllGroupsForNestedConstraints()
22022202

22032203
$violations = $this->validator->validate($entity, null, ['one', 'two']);
22042204

2205-
$this->assertCount(3, $violations);
2205+
$this->assertCount(2, $violations);
22062206
$this->assertInstanceOf(NotBlank::class, $violations->get(0)->getConstraint());
22072207
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
2208-
$this->assertInstanceOf(Length::class, $violations->get(2)->getConstraint());
22092208
}
22102209

22112210
public function testRequiredConstraintIsIgnored()

0 commit comments

Comments
 (0)