Skip to content

Commit 2dc28ea

Browse files
committed
bug symfony#16152 Fix URL validator failure with empty string (fabpot, bocharsky-bw)
This PR was merged into the 2.3 branch. Discussion ---------- Fix URL validator failure with empty string | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#15898 | License | MIT | Doc PR | ~ Fix validator failure if method `__toString` of passed object return an empty string. Commits ------- e0910d9 Fix URL validator failure with empty string 0f61859 [Validator] added a failing test
2 parents 1c43a4e + e0910d9 commit 2dc28ea

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class UrlValidator extends ConstraintValidator
4141
*/
4242
public function validate($value, Constraint $constraint)
4343
{
44-
if (null === $value || '' === $value) {
44+
if (null === $value) {
4545
return;
4646
}
4747

@@ -50,6 +50,10 @@ public function validate($value, Constraint $constraint)
5050
}
5151

5252
$value = (string) $value;
53+
if ('' === $value) {
54+
return;
55+
}
56+
5357
$pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));
5458

5559
if (!preg_match($pattern, $value)) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public function testEmptyStringIsValid()
3535
$this->assertNoViolation();
3636
}
3737

38+
public function testEmptyStringFromObjectIsValid()
39+
{
40+
$this->validator->validate(new EmailProvider(), new Url());
41+
42+
$this->assertNoViolation();
43+
}
44+
3845
/**
3946
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
4047
*/
@@ -171,3 +178,11 @@ public function getValidCustomUrls()
171178
);
172179
}
173180
}
181+
182+
class EmailProvider
183+
{
184+
public function __toString()
185+
{
186+
return '';
187+
}
188+
}

0 commit comments

Comments
 (0)