Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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 1 commit into from
Jun 2, 2022
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
7 changes: 7 additions & 0 deletions UPGRADE-6.2.md
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
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
---

Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

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?

*/
public const VALIDATION_MODE_LOOSE = 'loose';

public const INVALID_FORMAT_ERROR = 'bd79c0ab-ddba-46cc-a703-a7a4b08de310';
Expand Down Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The 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__));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function __construct(string $defaultMode = Email::VALIDATION_MODE_LOOSE)
throw new \InvalidArgumentException('The "defaultMode" parameter value is not valid.');
}

if (Email::VALIDATION_MODE_LOOSE === $defaultMode) {
trigger_deprecation('symfony/validator', '6.2', 'The "%s" mode is deprecated. The default mode will be changed to "%s" in 7.0.', Email::VALIDATION_MODE_LOOSE, Email::VALIDATION_MODE_HTML5);
}

$this->defaultMode = $defaultMode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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]'],
Expand All @@ -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"],
];
}

Expand Down Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions src/Symfony/Component/Validator/Tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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');
Expand All @@ -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());
}
}