8000 Fix URL validator failure with empty string by fabpot · Pull Request #16152 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fix URL validator failure with empty string #16152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Symfony/Component/Validator/Constraints/UrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class UrlValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
if (null === $value) {
return;
}

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

$value = (string) $value;
if ('' === $value) {
return;
}

$pattern = sprintf(static::PATTERN, implode('|', $constraint->protocols));

if (!preg_match($pattern, $value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public function testEmptyStringIsValid()
$this->assertNoViolation();
}

public function testEmptyStringFromObjectIsValid()
{
$this->validator->validate(new EmailProvider(), new Url());

$this->assertNoViolation();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
Expand Down Expand Up @@ -171,3 +178,11 @@ public function getValidCustomUrls()
);
}
}

class EmailProvider
{
public function __toString()
{
return '';
}
}
0