-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Html5 Email Validation #24442
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
Conversation
2ce7aed
to
5bc8189
Compare
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.
thanks for the PR!
some pure style comments for now to help next reviews
this PR should target 3.4 (rebase+github-retarget needed)
|
||
public function __construct($strict = false) | ||
public function __construct($mode = 'html5') |
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.
we need to make the change in a BC friendly way, so I think we cannot change the default behavior
true/false should still be handled as before as much as this makes sense
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.
Something along these lines?
/**
* @var string
*/
private $mode;
public function __construct($strict = false, $mode = 'html5')
{
$this->mode = $mode;
if($strict === true) {
$this->mode = Email::VALIDATION_MODE_STRICT;
}
}
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.
No, we should have only 1 argument (as we don't want to keep a first unused argument in the future API)
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(Email::INVALID_FORMAT_ERROR) | ||
->addViolation(); | ||
->setParameter('{{ value }}', $this->formatValue($value)) |
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.
please use single indentation level as before, we don't align ->
|
||
return; | ||
} elseif (!interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, false, true)) { |
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.
I would keep "strict" instead of "rfc" for maintenance purpose (will help when merging branches)
@@ -54,7 +54,6 @@ public function testExpectsStringCompatibleType() | |||
public function testValidEmails($email) | |||
{ | |||
$this->validator->validate($email, new Email()); | |||
|
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.
should be revert, to ease reviewing the patch
@@ -94,23 +93,23 @@ public function getInvalidEmails() | |||
); | |||
} | |||
|
|||
public function testStrict() | |||
public function testRfc() |
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.
same: let's keep strict everywhere to reduce the diff, thus help the review+future merges
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.
Note having any test for the new mode is wrong.
} | ||
|
||
if ($constraint->strict) { | ||
var_dump($constraint->mode); |
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.
should be removed.
|
||
return; | ||
} | ||
} elseif (!preg_match('/^.+\@\S+\.\S+$/', $value)) { | ||
} elseif (Email::VALIDATION_MODE_LOOSE === $constraint->mode && preg_match('/^.+\@\S+\.\S+$/', $value)) { |
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.
this is broken. You removed the !
, so considering any email with a @
in it as invalid.
->addViolation(); | ||
|
||
return; | ||
} elseif (Email::VALIDATION_MODE_HTML5 === $constraint->mode && preg_match( |
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.
missing negation too
{ | ||
$constraint = new Email(array('strict' => true)); |
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.
Some tests must be kept with the existing name, to cover backward compatibility (if this is deprecated, it should be come a legacy test, but it should not disappear)
public $message = 'This value is not a valid email address.'; | ||
public $checkMX = false; | ||
public $checkHost = false; | ||
public $strict; | ||
public $mode = 'html5'; |
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.
This is a BC break for 3 reasons:
- the
strict
property does not work anymore - the default behavior has changed)
- the constraint now has a default mode, meaning it is not possible to configure a global default anymore at the config level (as the validator uses it only when the constraint does not have a config)
fa2c775
to
05f981c
Compare
$this->defaultMode = $defaultMode; | ||
|
||
if (true === $strict) { | ||
$this->defaultMode = Email::VALIDATION_MODE_STRICT; |
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.
Not terribly happy with this. Suggestions?
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.
something like that yes (sorry for the CS)
public function __construct($defaultMode = Email::VALIDATION_MODE_STRICT)
{
switch ($defaultMode) {
case VALIDATION_MODE_HTML5:
case VALIDATION_MODE_STRICT:
case VALIDATION_MODE_LOOSE: break;
default: $defaultMode = $defaultMode ? VALIDATION_MODE_STRICT : VALIDATION_MODE_LOOSE;
}
$this->defaultMode = $defaultMode;
}
73b9c24
to
a8df1a2
Compare
a7af07e
to
8843696
Compare
Failing tests come from current 3.4 branch (as of 3.4 being at 98dae3e). |
We talked about including this on 3.4 and decided it was too late. As such, moving to 4.1. |
I'll rebase against that tonight |
8843696
to
1752ba0
Compare
Okay that's done now. (broken tests from master, again) |
1752ba0
to
ae51161
Compare
Time to merge @symfony/deciders ? |
@@ -21,6 +21,10 @@ | |||
*/ | |||
class Email extends Constraint | |||
{ | |||
const VALIDATION_MODE_HTML5 = 'html5'; | |||
const VALIDATION_MODE_STRICT = 'strict'; | |||
const VALIDATION_MODE_LOOSE = 'loose'; |
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.
What about being explicit now and markt these constants as public
?
public $message = 'This value is not a valid email address.'; | ||
public $checkMX = false; | ||
public $checkHost = false; | ||
|
||
/** | ||
* @deprecated since version 3.4, to be removed in 4.0. Set mode to "strict" instead. |
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.
@deprecated since version 4.1, to be removed in 5.0. Set mode to "strict" instead.
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 other messages need to be updated accordingly.
The deprecation also needs to be documented in |
I'll update those tonight, or at lunchtime if I get a chance 👍 |
879b4b4
to
9d5f021
Compare
That should be the version numbers corrected in the deprication notices |
9d5f021
to
7796364
Compare
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'`
7796364
to
cf04108
Compare
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.
Looks great to me!
Thank you @PurpleBooth. |
This PR was merged into the 4.1-dev branch. Discussion ---------- [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'` | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #21531 | License | MIT | Doc PR | symfony/symfony-docs#8487 <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the 3.4, legacy code removals go to the master branch. - Please fill in this template according to the PR you're about to submit. - Replace this comment by a description of what your PR is solving. --> Commits ------- cf04108 [Validator] Html5 Email Validation
This PR was merged into the master branch. Discussion ---------- [Validator] Html5 Email Validation Add documentation about the new mode parameter. Adds descriptions for the 'loose', 'strict', and 'html5' options. Relates to symfony/symfony#24442 Commits ------- 07d4bf9 [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 thevalidation that is applied to the field as a whole.
These modes are:
Deprecates the
strict=true
parameter in favour ofmode='strict'