10000 [Validator] Add email html5-force-tld mode with migration path · Sajito/symfony@fef5b08 · GitHub
[go: up one dir, main page]

Skip to content

Commit fef5b08

Browse files
author
Gigino Chianese
committed
[Validator] Add email html5-force-tld mode with migration path
1 parent a0b82ee commit fef5b08

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.2
55
---
66

7+
* Rename option `Email::VALIDATION_MODE_HTML5` to `Email::VALIDATION_MODE_HTML5_FORCE_TLD` with migration path, to make `Email::VALIDATION_MODE_HTML5` consistent with browser validation
78
* Add method `getCause()` to `ConstraintViolationInterface`
89
* Add the `When` constraint and validator
910
* Deprecate the "loose" e-mail validation mode, use "html5" instead

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
class Email extends Constraint
2727
{
2828
public const VALIDATION_MODE_HTML5 = 'html5';
29+
public const VALIDATION_MODE_HTML5_FORCE_TLD = 'html5-force-tld';
2930
public const VALIDATION_MODE_STRICT = 'strict';
3031
/**
3132
* @deprecated since Symfony 6.2
@@ -36,6 +37,7 @@ class Email extends Constraint
3637

3738
public const VALIDATION_MODES = [
3839
self::VALIDATION_MODE_HTML5,
40+
self::VALIDATION_MODE_HTML5_FORCE_TLD,
3941
self::VALIDATION_MODE_STRICT,
4042
self::VALIDATION_MODE_LOOSE,
4143
];
@@ -59,7 +61,9 @@ public function __construct(
5961
string $mode = null,
6062
callable $normalizer = null,
6163
array $groups = null,
62-
mixed $payload = null
64+
mixed $payload = null,
65+
/** @deprecated since Symfony 6.2, it's only needed to allow migrating html5 mode to html5-force-tld mode without immediate BC */
66+
bool $allowNoTld = null
6367
) {
6468
if (\is_array($options) && \array_key_exists('mode', $options) && !\in_array($options['mode'], self::VALIDATION_MODES, true)) {
6569
throw new InvalidArgumentException('The "mode" parameter value is not valid.');
@@ -71,8 +75,26 @@ public function __construct(
7175
$this->mode = $mode ?? $this->mode;
7276
$this->normalizer = $normalizer ?? $this->normalizer;
7377

78+
if (self::VALIDATION_MODE_HTML5 === $mode && !$allowNoTld) {
79+
trigger_deprecation(
80+
'symfony/validator',
81+
'6.2',
82+
sprintf('To match browser validation, the "html5" mode will allow domains without tld. Use "%s" to keep disallowing those domains.', self::VALIDATION_MODE_HTML5_FORCE_TLD)
83+
);
84+
85+
$this->mode = self::VALIDATION_MODE_HTML5_FORCE_TLD;
86+
}
87+
88+
if (null !== $allowNoTld) {
89+
trigger_deprecation(
90+
'symfony/validator',
91+
'6.2',
92+
sprintf('$allowNoTld flag is only included temporarily, please remove it\'s usages during updates to 7.0.')
93+
);
94+
}
95+
7496
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);
97+
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_FORCE_TLD);
7698
}
7799

78100
if (self::VALIDATION_MODE_STRICT === $this->mode && !class_exists(StrictEmailValidator::class)) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
*/
2525
class EmailValidator extends ConstraintValidator
2626
{
27-
private 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])?)+$/';
27+
private 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])?)*$/';
28+
private const PATTERN_HTML5_FORCE_TLD = '/^[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])?)+$/';
2829
private const PATTERN_LOOSE = '/^.+\@\S+\.\S+$/';
2930

3031
private const EMAIL_PATTERNS = [
3132
Email::VALIDATION_MODE_LOOSE => self::PATTERN_LOOSE,
3233
Email::VALIDATION_MODE_HTML5 => self::PATTERN_HTML5,
34+
Email::VALIDATION_MODE_HTML5_FORCE_TLD => self::PATTERN_HTML5_FORCE_TLD,
3335
];
3436

3537
private string $defaultMode;
@@ -41,7 +43,7 @@ public function __construct(string $defaultMode = Email::VALIDATION_MODE_LOOSE)
4143
}
4244

4345
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);
46+
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_FORCE_TLD);
4547
}
4648

4749
$this->defaultMode = $defaultMode;

0 commit comments

Comments
 (0)
0