8000 [Validator] Allow empty string on LengthValidator · symfony/symfony@ab67e21 · GitHub
[go: up one dir, main page]

Skip to content

Commit ab67e21

Browse files
committed
[Validator] Allow empty string on LengthValidator
Allow empty string if "min" option is null to match documentation "As with most of the other constraints, null and empty strings are considered valid values".
1 parent 7e13694 commit ab67e21

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
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 && !$constraint->min)) {
3434
return;
3535
}
3636

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ public function testNullIsValid()
3030
$this->assertNoViolation();
3131
}
3232

33-
public function testEmptyStringIsInvalid()
33+
public function testEmptyStringIsValidWithoutMin()
34+
{
35+
$this->validator->validate('', new Length(['max' => 6]));
36+
37+
$this->assertNoViolation();
38+
}
39+
40+
public function testEmptyStringIsInvalidWithMin()
3441
{
3542
$this->validator->validate('', new Length([
3643
'value' => $limit = 6,

0 commit comments

Comments
 (0)
0