-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[Validator] #18156 In "strict" mode, email validator inaccurately cla… #18428
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f69769d
[Validator] #18156 In "strict" mode, email validator inaccurately cla…
natechicago 5a7f775
[Validator] #18156 In "strict" mode, email validator inaccurately cla…
natechicago cf5d97f
[Validator] #18156 In "strict" mode, email validator inaccurately cla…
natechicago 8b448d7
[Validator] #18156 In "strict" mode, email validator inaccurately cla…
natechicago File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,25 @@ | |
class EmailValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* @var bool | ||
* @var string | ||
*/ | ||
private $isStrict; | ||
private $defaultProfile; | ||
|
||
public function __construct($strict = false) | ||
/** | ||
* @param bool $strict Deprecated. If the constraint does not define | ||
* a validation profile, this will determine if | ||
* 'rfc-no-warn' or 'basic' should be used as | ||
* the default profile. | ||
* @param string|null $profile If the constraint does not define a validation | ||
* profile, this will specify which profile to use. | ||
*/ | ||
public function __construct($strict = false, $profile = null) | ||
{ | ||
$this->isStrict = $strict; | ||
$this->defaultProfile = (null === $profile) | ||
? $strict | ||
? Email::PROFILE_RFC_DISALLOW_WARNINGS | ||
: Email::PROFILE_BASIC_REGEX | ||
: $profile; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
} | ||
|
||
/** | ||
|
@@ -50,26 +62,42 @@ public function validate($value, Constraint $constraint) | |
|
||
$value = (string) $value; | ||
|
||
if (null === $constraint->strict) { | ||
$constraint->strict = $this->isStrict; | ||
if (isset($constraint->strict)) { | ||
$constraint->profile = $constraint->strict | ||
? Email::PROFILE_RFC_DISALLOW_WARNINGS | ||
: Email::PROFILE_BASIC_REGEX; | ||
} | ||
|
||
if ($constraint->strict) { | ||
if (!class_exists('\Egulias\EmailValidator\EmailValidator')) { | ||
throw new RuntimeException('Strict email validation requires egulias/email-validator'); | ||
} | ||
|
||
$strictValidator = new \Egulias\EmailValidator\EmailValidator(); | ||
if (null === $constraint->profile) { | ||
$constraint->profile = $this->defaultProfile; | ||
} | ||
|
||
if (!$strictValidator->isValid($value, false, true)) { | ||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(Email::INVALID_FORMAT_ERROR) | ||
->addViolation(); | ||
// Determine if the email address is valid | ||
switch ($constraint->profile) { | ||
case Email::PROFILE_BASIC_REGEX: | ||
case Email::PROFILE_HTML5_REGEX: | ||
$regex = (Email::PROFILE_BASIC_REGEX === $constraint->profile) | ||
? '/^.+\@\S+\.\S+$/' | ||
: '/^[a-zA-Z0-9.!#$%&’*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/'; | ||
$emailAddressIsValid = (bool) preg_match($regex, $value); | ||
break; | ||
case Email::PROFILE_RFC_ALLOW_WARNINGS: | ||
case Email::PROFILE_RFC_DISALLOW_WARNINGS: | ||
if (!class_exists('\Egulias\EmailValidator\EmailValidator')) { | ||
throw new RuntimeException('Standards-compliant email validation requires egulias/email-validator'); | ||
} | ||
$rfcValidator = new \Egulias\EmailValidator\EmailValidator(); | ||
$emailAddressIsValid = $rfcValidator->isValid( | ||
$value, | ||
false, | ||
Email::PROFILE_RFC_DISALLOW_WARNINGS === $constraint->profile | ||
); | ||
break; | ||
default: | ||
throw new RuntimeException('Unrecognized email validation profile'); | ||
} | ||
|
||
return; | ||
} | ||
} elseif (!preg_match('/^.+\@\S+\.\S+$/', $value)) { | ||
if (!$emailAddressIsValid) { | ||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(Email::INVALID_FORMAT_ERROR) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The removal of the
strict
option must be documented in theUPGRADE_4.0.md
file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we will need to trigger deprecations.