From de5d68ef2a6defc2cfd68b5d7a91635bec81451d Mon Sep 17 00:00:00 2001 From: Jeroen Thora Date: Sun, 3 May 2020 21:30:24 +0200 Subject: [PATCH] Skip validation when email is an empty object --- .../Validator/Constraints/EmailValidator.php | 3 +++ .../Tests/Constraints/EmailValidatorTest.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Symfony/Component/Validator/Constraints/EmailValidator.php b/src/Symfony/Component/Validator/Constraints/EmailValidator.php index d0eaa4540227..58b372d988dd 100644 --- a/src/Symfony/Component/Validator/Constraints/EmailValidator.php +++ b/src/Symfony/Component/Validator/Constraints/EmailValidator.php @@ -51,6 +51,9 @@ public function validate($value, Constraint $constraint) } $value = (string) $value; + if ('' === $value) { + return; + } if (null === $constraint->strict) { $constraint->strict = $this->isStrict; diff --git a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php index 344139a44f17..9299c7efad2e 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php @@ -40,6 +40,13 @@ public function testEmptyStringIsValid() $this->assertNoViolation(); } + public function testObjectEmptyStringIsValid() + { + $this->validator->validate(new EmptyEmailObject(), new Email()); + + $this->assertNoViolation(); + } + public function testExpectsStringCompatibleType() { $this->expectException('Symfony\Component\Validator\Exception\UnexpectedTypeException'); @@ -256,3 +263,11 @@ public function provideCheckTypes() ]; } } + +class EmptyEmailObject +{ + public function __toString() + { + return ''; + } +}