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

Skip to content

Commit bc3cc59

Browse files
committed
deprecate the loose e-mail validation mode
1 parent f4361fb commit bc3cc59

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed

UPGRADE-6.2.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
UPGRADE FROM 6.1 to 6.2
2+
=======================
3+
4+
Validator
5+
---------
6+
7+
* Deprecate the "loose" e-mail validation mode, use "html5" instead

src/Symfony/Component/Validator/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

src/Symfony/Component/Validator/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 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
}

src/Symfony/Component/Validator/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

src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php

Lines changed: 44 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,23 @@ public function getValidEmails()
7679
7780
7881
82+
];
83+
}
84+
85+
/**
86+
* @group legacy
87+
* @dataProvider getValidInLooseModeEmails
88+
*/
89+
public function testValidInLooseModeEmails($email)
90+
{
91+
$this->validator->validate($email, new Email(['mode' => Email::VALIDATION_MODE_LOOSE]));
92+
93+
$this->assertNoViolation();
94+
}
95+
96+
public function getValidInLooseModeEmails()
97+
{
98+
return [
7999
80100
['{}~!@!@£$%%^&*().!@£$%^&*()'],
81101
@@ -98,11 +118,28 @@ public function getValidEmailsWithWhitespaces()
98118
{
99119
return [
100120
["\x20[email protected]\x20"],
121+
["[email protected]\x0B\x0B"],
122+
];
123+
}
124+
125+
/**
126+
* @group legacy
127+
* @dataProvider getValidEmailsInLooseModeWithWhitespaces
128+
*/
129+
public function testValidNormalizedEmailsInLooseMode($email)
130+
{
131+
$this->validator->validate($email, new Email(['mode' => Email::VALIDATION_MODE_LOOSE, 'normalizer' => 'trim']));
132+
133+
$this->assertNoViolation();
134+
}
135+
136+
public function getValidEmailsInLooseModeWithWhitespaces()
137+
{
138+
return [
101139
["\x09\x09[email protected]\x09\x09"],
102140
["\x0A{}~!@!@£$%%^&*().!@£$%^&*()\x0A"],
103141
["\x0D\x0D[email protected]\x0D\x0D"],
104142
105-
["[email protected]\x0B\x0B"],
106143
];
107144
}
108145

@@ -214,8 +251,13 @@ public function testModeHtml5()
214251
->assertRaised();
215252
}
216253

254+
/**
255+
* @group legacy
256+
*/
217257
public function testModeLoose()
218258
{
259+
$this->expectDeprecation('Since symfony/validator 6.2: The "loose" mode is deprecated. The default mode will be changed to "html5" in 7.0.');
260+
219261
$constraint = new Email(['mode' => Email::VALIDATION_MODE_LOOSE]);
220262

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

src/Symfony/Component/Validator/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)