-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] deprecate the loose e-mail validation mode #46518
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
UPGRADE FROM 6.1 to 6.2 | ||
======================= | ||
|
||
Validator | ||
--------- | ||
|
||
* Deprecate the "loose" e-mail validation mode, use "html5" instead |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.2 | ||
--- | ||
|
||
* Deprecate the "loose" e-mail validation mode, use "html5" instead | ||
|
||
6.1 | ||
--- | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,9 @@ class Email extends Constraint | |
{ | ||
public const VALIDATION_MODE_HTML5 = 'html5'; | ||
public const VALIDATION_MODE_STRICT = 'strict'; | ||
/** | ||
* @deprecated since Symfony 6.2 | ||
*/ | ||
public const VALIDATION_MODE_LOOSE = 'loose'; | ||
|
||
public const INVALID_FORMAT_ERROR = 'bd79c0ab-ddba-46cc-a703-a7a4b08de310'; | ||
|
@@ -68,6 +71,10 @@ public function __construct( | |
$this->mode = $mode ?? $this->mode; | ||
$this->normalizer = $normalizer ?? $this->normalizer; | ||
|
||
if (self::VALIDATION_MODE_LOOSE === $this->mode) { | ||
trigger_deprecation('symfony/validator', '6.2', 'The "%s" mode is deprecated. The default mode will be changed to "%s" in 7.0.', self::VALIDATION_MODE_LOOSE, self::VALIDATION_MODE_HTML5); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the default be changed or will the validation mode be removed? |
||
} | ||
|
||
if (self::VALIDATION_MODE_STRICT === $this->mode && !class_exists(StrictEmailValidator::class)) { | ||
throw new LogicException(sprintf('The "egulias/email-validator" component is required to use the "%s" constraint in strict mode.', __CLASS__)); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
namespace Symfony\Component\Validator\Tests\Constraints; | ||
|
||
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; | ||
use Symfony\Component\Validator\Constraints\Email; | ||
use Symfony\Component\Validator\Constraints\EmailValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedValueException; | ||
|
@@ -21,9 +22,11 @@ | |
*/ | ||
class EmailValidatorTest extends ConstraintValidatorTestCase | ||
{ | ||
use ExpectDeprecationTrait; | ||
|
||
protected function createValidator() | ||
{ | ||
return new EmailValidator(Email::VALIDATION_MODE_LOOSE); | ||
return new EmailValidator(Email::VALIDATION_MODE_HTML5); | ||
} | ||
|
||
public function testUnknownDefaultModeTriggerException() | ||
|
@@ -76,6 +79,24 @@ public function getValidEmails() | |
['[email protected]'], | ||
['[email protected]'], | ||
['[email protected]'], | ||
]; | ||
} | ||
|
||
/** | ||
* @group legacy | ||
* @dataProvider getValidEmails | ||
* @dataProvider getEmailsOnlyValidInLooseMode | ||
*/ | ||
public function testValidInLooseModeEmails($email) | ||
{ | ||
$this->validator->validate($email, new Email(['mode' => Email::VALIDATION_MODE_LOOSE])); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function getEmailsOnlyValidInLooseMode() | ||
{ | ||
return [ | ||
['[email protected]'], | ||
['{}~!@!@£$%%^&*().!@£$%^&*()'], | ||
['[email protected]'], | ||
|
@@ -98,11 +119,29 @@ public function getValidEmailsWithWhitespaces() | |
{ | ||
return [ | ||
["\[email protected]\x20"], | ||
["[email protected]\x0B\x0B"], | ||
]; | ||
} | ||
|
||
/** | ||
* @group legacy | ||
* @dataProvider getValidEmailsWithWhitespaces | ||
* @dataProvider getEmailsWithWhitespacesOnlyValidInLooseMode | ||
*/ | ||
public function testValidNormalizedEmailsInLooseMode($email) | ||
{ | ||
$this->validator->validate($email, new Email(['mode' => Email::VALIDATION_MODE_LOOSE, 'normalizer' => 'trim'])); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function getEmailsWithWhitespacesOnlyValidInLooseMode() | ||
{ | ||
return [ | ||
["\x09\[email protected]\x09\x09"], | ||
["\x0A{}~!@!@£$%%^&*().!@£$%^&*()\x0A"], | ||
["\x0D\[email protected]\x0D\x0D"], | ||
["\[email protected]"], | ||
["[email protected]\x0B\x0B"], | ||
]; | ||
} | ||
|
||
|
@@ -214,8 +253,13 @@ public function testModeHtml5() | |
->assertRaised(); | ||
} | ||
|
||
/** | ||
* @group legacy | ||
*/ | ||
public function testModeLoose() | ||
{ | ||
$this->expectDeprecation('Since symfony/validator 6.2: The "loose" mode is deprecated. The default mode will be changed to "html5" in 7.0.'); | ||
|
||
$constraint = new Email(['mode' => Email::VALIDATION_MODE_LOOSE]); | ||
|
||
$this->validator->validate('[email protected]', $constraint); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ | |
namespace Symfony\Component\Validator\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\Constraints\Email; | ||
use Symfony\Component\Validator\Constraints\Blank; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
use Symfony\Component\Validator\Exception\ValidationFailedException; | ||
use Symfony\Component\Validator\Validation; | ||
|
||
|
@@ -23,13 +24,13 @@ class ValidationTest extends TestCase | |
{ | ||
public function testCreateCallableValid() | ||
{ | ||
$validator = Validation::createCallable(new Email()); | ||
$validator = Validation::createCallable(new NotBlank()); | ||
$this->assertEquals('[email protected]', $validator('[email protected]')); | ||
} | ||
|
||
public function testCreateCallableInvalid() | ||
{ | ||
$validator = Validation::createCallable(new Email()); | ||
$validator = Validation::createCallable(new Blank()); | ||
try { | ||
$validator('test'); | ||
$this->fail('No ValidationFailedException thrown'); | ||
|
@@ -38,21 +39,21 @@ public function testCreateCallableInvalid() | |
|
||
$violations = $e->getViolations(); | ||
$this->assertCount(1, $violations); | ||
$this->assertEquals('This value is not a valid email address.', $violations->get(0)->getMessage()); | ||
$this->assertEquals('This value should be blank.', $violations->get(0)->getMessage()); | ||
} | ||
} | ||
|
||
public function testCreateIsValidCallableValid() | ||
{ | ||
$validator = Validation::createIsValidCallable(new Email()); | ||
$validator = Validation::createIsValidCallable(new NotBlank()); | ||
$this->assertTrue($validator('[email protected]')); | ||
} | ||
|
||
public function testCreateIsValidCallableInvalid() | ||
{ | ||
$validator = Validation::createIsValidCallable(new Email()); | ||
$validator = Validation::createIsValidCallable(new Blank()); | ||
$this->assertFalse($validator('test', $violations)); | ||
$this->assertCount(1, $violations); | ||
$this->assertEquals('This value is not a valid email address.', $violations->get(0)->getMessage()); | ||
$this->assertEquals('This value should be blank.', $violations->get(0)->getMessage()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@deprecated since Symfony 6.2, use VALIDATION_MODE_HTML5 instead?