Skip to content

Commit c7e2dd0

Browse files
committed
deprecate the loose e-mail validation mode
1 parent afd5c69 commit c7e2dd0

File tree

5 files changed

+70
-9
lines changed

5 files changed

+70
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Deprecate the "loose" e-mail validation mode, use "html5" instead
8+
49
6.1
510
---
611

Constraints/Email.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class Email extends Constraint
2727
{
2828
public const VALIDATION_MODE_HTML5 = 'html5';
2929
public const VALIDATION_MODE_STRICT = 'strict';
30+
/**
31+
* @deprecated since Symfony 6.2
32+
*/
3033
public const VALIDATION_MODE_LOOSE = 'loose';
3134

3235
public const INVALID_FORMAT_ERROR = 'bd79c0ab-ddba-46cc-a703-a7a4b08de310';
@@ -68,6 +71,10 @@ public function __construct(
6871
$this->mode = $mode ?? $this->mode;
6972
$this->normalizer = $normalizer ?? $this->normalizer;
7073

74+
if (self::VALIDATION_MODE_LOOSE === $this->mode) {
75+
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);
76+
}
77+
7178
if (self::VALIDATION_MODE_STRICT === $this->mode && !class_exists(StrictEmailValidator::class)) {
7279
throw new LogicException(sprintf('The "egulias/email-validator" component is required to use the "%s" constraint in strict mode.', __CLASS__));
7380
}

Constraints/EmailValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function __construct(string $defaultMode = Email::VALIDATION_MODE_LOOSE)
4040
throw new \InvalidArgumentException('The "defaultMode" parameter value is not valid.');
4141
}
4242

43+
if (Email::VALIDATION_MODE_LOOSE === $defaultMode) {
44+
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);
45+
}
46+
4347
$this->defaultMode = $defaultMode;
4448
}
4549

Tests/Constraints/EmailValidatorTest.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1415
use Symfony\Component\Validator\Constraints\Email;
1516
use Symfony\Component\Validator\Constraints\EmailValidator;
1617
use Symfony\Component\Validator\Exception\UnexpectedValueException;
@@ -21,9 +22,11 @@
2122
*/
2223
class EmailValidatorTest extends ConstraintValidatorTestCase
2324
{
25+
use ExpectDeprecationTrait;
26+
2427
protected function createValidator()
2528
{
26-
return new EmailValidator(Email::VALIDATION_MODE_LOOSE);
29+
return new EmailValidator(Email::VALIDATION_MODE_HTML5);
2730
}
2831

2932
public function testUnknownDefaultModeTriggerException()
@@ -76,6 +79,24 @@ public function getValidEmails()
7679
7780
7881
82+
];
83+
}
84+
85+
/**
86+
* @group legacy
87+
* @dataProvider getValidEmails
88+
* @dataProvider getEmailsOnlyValidInLooseMode
89+
*/
90+
public function testValidInLooseModeEmails($email)
91+
{
92+
$this->validator->validate($email, new Email(['mode' => Email::VALIDATION_MODE_LOOSE]));
93+
94+
$this->assertNoViolation();
95+
}
96+
97+
public function getEmailsOnlyValidInLooseMode()
98+
{
99+
return [
79100
80101
['{}~!@!@£$%%^&*().!@£$%^&*()'],
81102
@@ -98,11 +119,29 @@ public function getValidEmailsWithWhitespaces()
98119
{
99120
return [
100121
["\x20[email protected]\x20"],
122+
["[email protected]\x0B\x0B"],
123+
];
124+
}
125+
126+
/**
127+
* @group legacy
128+
* @dataProvider getValidEmailsWithWhitespaces
129+
* @dataProvider getEmailsWithWhitespacesOnlyValidInLooseMode
130+
*/
131+
public function testValidNormalizedEmailsInLooseMode($email)
132+
{
133+
$this->validator->validate($email, new Email(['mode' => Email::VALIDATION_MODE_LOOSE, 'normalizer' => 'trim']));
134+
135+
$this->assertNoViolation();
136+
}
137+
138+
public function getEmailsWithWhitespacesOnlyValidInLooseMode()
139+
{
140+
return [
101141
["\x09\x09[email protected]\x09\x09"],
102142
["\x0A{}~!@!@£$%%^&*().!@£$%^&*()\x0A"],
103143
["\x0D\x0D[email protected]\x0D\x0D"],
104144
105-
["[email protected]\x0B\x0B"],
106145
];
107146
}
108147

@@ -214,8 +253,13 @@ public function testModeHtml5()
214253
->assertRaised();
215254
}
216255

256+
/**
257+
* @group legacy
258+
*/
217259
public function testModeLoose()
218260
{
261+
$this->expectDeprecation('Since symfony/validator 6.2: The "loose" mode is deprecated. The default mode will be changed to "html5" in 7.0.');
262+
219263
$constraint = new Email(['mode' => Email::VALIDATION_MODE_LOOSE]);
220264

221265
$this->validator->validate('[email protected]', $constraint);

Tests/ValidationTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace Symfony\Component\Validator\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Validator\Constraints\Email;
15+
use Symfony\Component\Validator\Constraints\Blank;
16+
use Symfony\Component\Validator\Constraints\NotBlank;
1617
use Symfony\Component\Validator\Exception\ValidationFailedException;
1718
use Symfony\Component\Validator\Validation;
1819

@@ -23,13 +24,13 @@ class ValidationTest extends TestCase
2324
{
2425
public function testCreateCallableValid()
2526
{
26-
$validator = Validation::createCallable(new Email());
27+
$validator = Validation::createCallable(new NotBlank());
2728
$this->assertEquals('[email protected]', $validator('[email protected]'));
2829
}
2930

3031
public function testCreateCallableInvalid()
3132
{
32-
$validator = Validation::createCallable(new Email());
33+
$validator = Validation::createCallable(new Blank());
3334
try {
3435
$validator('test');
3536
$this->fail('No ValidationFailedException thrown');
@@ -38,21 +39,21 @@ public function testCreateCallableInvalid()
3839

3940
$violations = $e->getViolations();
4041
$this->assertCount(1, $violations);
41-
$this->assertEquals('This value is not a valid email address.', $violations->get(0)->getMessage());
42+
$this->assertEquals('This value should be blank.', $violations->get(0)->getMessage());
4243
}
4344
}
4445

4546
public function testCreateIsValidCallableValid()
4647
{
47-
$validator = Validation::createIsValidCallable(new Email());
48+
$validator = Validation::createIsValidCallable(new NotBlank());
4849
$this->assertTrue($validator('[email protected]'));
4950
}
5051

5152
public function testCreateIsValidCallableInvalid()
5253
{
53-
$validator = Validation::createIsValidCallable(new Email());
54+
$validator = Validation::createIsValidCallable(new Blank());
5455
$this->assertFalse($validator('test', $violations));
5556
$this->assertCount(1, $violations);
56-
$this->assertEquals('This value is not a valid email address.', $violations->get(0)->getMessage());
57+
$this->assertEquals('This value should be blank.', $violations->get(0)->getMessage());
5758
}
5859
}

0 commit comments

Comments
 (0)