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

Skip to content

Commit 8843696

Browse files
committed
[Validator] Html5 Email Validation
Currently we only support a very loose validation. There is now a standard HTML5 element with matching regex. This will add the ability to set a `mode` on the email validator. The mode will change the validation that is applied to the field as a whole. These modes are: * loose: The pattern from previous Symfony versions (default) * strict: Strictly matching the RFC * html5: The regex used for the HTML5 Element Deprecates the `strict=true` parameter in favour of `mode='strict'`
1 parent 98dae3e commit 8843696

File tree

5 files changed

+313
-10
lines changed

5 files changed

+313
-10
lines changed

UPGRADE-3.4.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,53 @@ Validator
413413
* Not setting the `strict` option of the `Choice` constraint to `true` is
414414
deprecated and will throw an exception in Symfony 4.0.
415415

416+
* The `strict` option of the `Email` constraint is deprecated and will
417+
be removed in Symfony 4.0, use the `mode` option instead.
418+
419+
Before:
420+
421+
```php
422+
use Symfony\Component\Validator\Constraints as Assert;
423+
424+
/**
425+
* @Assert\Email(strict=true)
426+
*/
427+
private $property;
428+
```
429+
430+
After:
431+
432+
```php
433+
use Symfony\Component\Validator\Constraints as Assert;
434+
435+
/**
436+
* @Assert\Email(mode="strict")
437+
*/
438+
private $property;
439+
```
440+
441+
* Calling the `EmailValidator` with a boolean constructor argument is deprecated and will
442+
be removed in Symfony 4.0.
443+
444+
Before:
445+
446+
```php
447+
use Symfony\Component\Validator\Constraints\EmailValidator;
448+
449+
$strictValidator = new EmailValidator(true);
450+
$looseValidator = new EmailValidator(false);
451+
```
452+
453+
After:
454+
455+
```php
456+
use Symfony\Component\Validator\Constraints\Email;
457+
use Symfony\Component\Validator\Constraints\EmailValidator;
458+
459+
$strictValidator = new EmailValidator(Email::VALIDATION_MODE_STRICT);
460+
$looseValidator = new EmailValidator(Email::VALIDATION_MODE_LOOSE);
461+
```
462+
416463
Yaml
417464
----
418465

src/Symfony/Component/Validator/Constraints/Email.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
*/
2222
class Email extends Constraint
2323
{
24+
const VALIDATION_MODE_HTML5 = 'html5';
25+
const VALIDATION_MODE_STRICT = 'strict';
26+
const VALIDATION_MODE_LOOSE = 'loose';
27+
2428
const INVALID_FORMAT_ERROR = 'bd79c0ab-ddba-46cc-a703-a7a4b08de310';
2529
const MX_CHECK_FAILED_ERROR = 'bf447c1c-0266-4e10-9c6c-573df282e413';
2630
const HOST_CHECK_FAILED_ERROR = '7da53a8b-56f3-4288-bb3e-ee9ede4ef9a1';
@@ -31,8 +35,37 @@ class Email extends Constraint
3135
self::HOST_CHECK_FAILED_ERROR => 'HOST_CHECK_FAILED_ERROR',
3236
);
3337

38+
/**
39+
* @var string[]
40+
*
41+
* @internal
42+
*/
43+
public static $validationModes = array(
44+
self::VALIDATION_MODE_HTML5,
45+
self::VALIDATION_MODE_STRICT,
46+
self::VALIDATION_MODE_LOOSE,
47+
);
48+
3449
public $message = 'This value is not a valid email address.';
3550
public $checkMX = false;
3651
public $checkHost = false;
52+
53+
/**
54+
* @deprecated since version 3.4, to be removed in 4.0. Set mode to "strict" instead.
55+
*/
3756
public $strict;
57+
public $mode;
58+
59+
public function __construct($options = null)
60+
{
61+
if (is_array($options) && array_key_exists('strict', $options)) {
62+
@trigger_error(sprintf('The \'strict\' property is deprecated since version 3.4 and will be removed in 4.0. Use \'mode\'=>"%s" instead.', self::VALIDATION_MODE_STRICT), E_USER_DEPRECATED);
63+
}
64+
65+
if (is_array($options) && array_key_exists('mode', $options) && !in_array($options['mode'], self::$validationModes, true)) {
66+
throw new \InvalidArgumentException('The \'mode\' parameter value is not valid.');
67+
}
68+
69+
parent::__construct($options);
70+
}
3871
}

src/Symfony/Component/Validator/Constraints/EmailValidator.php

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,40 @@
2424
class EmailValidator extends ConstraintValidator
2525
{
2626
/**
27-
* @var bool
27+
* @internal
2828
*/
29-
private $isStrict;
29+
const PATTERN_HTML5 = '/^[a-zA-Z0-9.!#$%&\'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/';
30+
/**
31+
* @internal
32+
*/
33+
const PATTERN_LOOSE = '/^.+\@\S+\.\S+$/';
34+
35+
private static $emailPatterns = array(
36+
Email::VALIDATION_MODE_LOOSE => self::PATTERN_LOOSE,
37+
Email::VALIDATION_MODE_HTML5 => self::PATTERN_HTML5,
38+
);
39+
40+
/**
41+
* @var string
42+
*/
43+
private $defaultMode;
3044

31-
public function __construct($strict = false)
45+
/**
46+
* @param string $defaultMode
47+
*/
48+
public function __construct($defaultMode = Email::VALIDATION_MODE_LOOSE)
3249
{
33-
$this->isStrict = $strict;
50+
if (is_bool($defaultMode)) {
51+
@trigger_error(sprintf('Calling `new %s(%s)` is deprecated since version 3.4 and will be removed in 4.0, use `new %s("%s")` instead.', self::class, $defaultMode ? 'true' : 'false', self::class, $defaultMode ? Email::VALIDATION_MODE_STRICT : Email::VALIDATION_MODE_LOOSE), E_USER_DEPRECATED);
52+
53+
$defaultMode = $defaultMode ? Email::VALIDATION_MODE_STRICT : Email::VALIDATION_MODE_LOOSE;
54+
}
55+
56+
if (!in_array($defaultMode, Email::$validationModes, true)) {
57+
throw new \InvalidArgumentException('The "defaultMode" parameter value is not valid.');
58+
}
59+
60+
$this->defaultMode = $defaultMode;
3461
}
3562

3663
/**
@@ -52,11 +79,25 @@ public function validate($value, Constraint $constraint)
5279

5380
$value = (string) $value;
5481

55-
if (null === $constraint->strict) {
56-
$constraint->strict = $this->isStrict;
82+
if (null !== $constraint->strict) {
83+
@trigger_error(sprintf('The %s::$strict property is deprecated since version 3.4 and will be removed in 4.0. Use %s::mode="%s" instead.', Email::class, Email::class, Email::VALIDATION_MODE_STRICT), E_USER_DEPRECATED);
84+
85+
if ($constraint->strict) {
86+
$constraint->mode = Email::VALIDATION_MODE_STRICT;
87+
} else {
88+
$constraint->mode = Email::VALIDATION_MODE_LOOSE;
89+
}
90+
}
91+
92+
if (null === $constraint->mode) {
93+
$constraint->mode = $this->defaultMode;
94+
}
95+
96+
if (!in_array($constraint->mode, Email::$validationModes, true)) {
97+
throw new \InvalidArgumentException(sprintf('The %s::$mode parameter value is not valid.', get_class($constraint)));
5798
}
5899

59-
if ($constraint->strict) {
100+
if (Email::VALIDATION_MODE_STRICT === $constraint->mode) {
60101
if (!class_exists('\Egulias\EmailValidator\EmailValidator')) {
61102
throw new RuntimeException('Strict email validation requires egulias/email-validator ~1.2|~2.0');
62103
}
@@ -78,7 +119,7 @@ public function validate($value, Constraint $constraint)
78119

79120
return;
80121
}
81-
} elseif (!preg_match('/^.+\@\S+\.\S+$/', $value)) {
122+
} elseif (!preg_match(self::$emailPatterns[$constraint->mode], $value)) {
82123
$this->context->buildViolation($constraint->message)
83124
->setParameter('{{ value }}', $this->formatValue($value))
84125
->setCode(Email::INVALID_FORMAT_ERROR)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Email;
16+
use Symfony\Component\Validator\Constraints\File;
17+
18+
class EmailTest extends TestCase
19+
{
20+
/**
21+
* @expectedDeprecation The 'strict' property is deprecated since version 3.4 and will be removed in 4.0. Use 'mode'=>"strict" instead.
22+
* @group legacy
23+
*/
24+
public function testLegacyConstructorStrict()
25+
{
26+
$subject = new Email(array('strict' => true));
27+
28+
$this->assertTrue($subject->strict);
29+
}
30+
31+
public function testConstructorStrict()
32+
{
33+
$subject = new Email(array('mode' => Email::VALIDATION_MODE_STRICT));
34+
35+
$this->assertEquals(Email::VALIDATION_MODE_STRICT, $subject->mode);
36+
}
37+
38+
/**
39+
* @expectedException \InvalidArgumentException
40+
* @expectedExceptionMessage The 'mode' parameter value is not valid.
41+
*/
42+
public function testUnknownModesTriggerException()
43+
{
44+
new Email(array('mode' => 'Unknown Mode'));
45+
}
46+
}

0 commit comments

Comments
 (0)