Skip to content

[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

Merged
merged 1 commit into from
Dec 11, 2017

Conversation

PurpleBooth
Copy link
Contributor

@PurpleBooth PurpleBooth commented Oct 5, 2017

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

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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')
Copy link
Member

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

Copy link
Contributor Author

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;
        }
    }

Copy link
Member

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))
Copy link
Member

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)) {
Copy link
Member

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());

Copy link
Member

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()
Copy link
Member

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

Copy link
Member

@stof stof left a 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);
Copy link
Member

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)) {
Copy link
Member

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(
Copy link
Member

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));
Copy link
Member

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';
Copy link
Member

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)

@stof stof added this to the 4.1 milestone Oct 5, 2017
@PurpleBooth PurpleBooth force-pushed the optional-email-filter branch 4 times, most recently from fa2c775 to 05f981c Compare October 5, 2017 16:50
$this->defaultMode = $defaultMode;

if (true === $strict) {
$this->defaultMode = Email::VALIDATION_MODE_STRICT;
Copy link
Contributor Author

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?

Copy link
Member

@nicolas-grekas nicolas-grekas Oct 5, 2017

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;
}

@PurpleBooth PurpleBooth force-pushed the optional-email-filter branch 9 times, most recently from 73b9c24 to a8df1a2 Compare October 5, 2017 17:16
@PurpleBooth PurpleBooth force-pushed the optional-email-filter branch 2 times, most recently from a7af07e to 8843696 Compare October 29, 2017 09:29
@PurpleBooth
Copy link
Contributor Author

PurpleBooth commented Oct 29, 2017

Failing tests come from current 3.4 branch (as of 3.4 being at 98dae3e).

@nicolas-grekas
Copy link
Member

We talked about including this on 3.4 and decided it was too late. As such, moving to 4.1.

@nicolas-grekas nicolas-grekas modified the milestones: 3.4, 4.1 Oct 30, 2017
@nicolas-grekas nicolas-grekas changed the base branch from 3.4 to master October 30, 2017 14:10
@PurpleBooth
Copy link
Contributor Author

I'll rebase against that tonight

@PurpleBooth PurpleBooth force-pushed the optional-email-filter branch from 8843696 to 1752ba0 Compare October 30, 2017 23:29
@PurpleBooth
Copy link
Contributor Author

Okay that's done now. (broken tests from master, again)

@PurpleBooth PurpleBooth force-pushed the optional-email-filter branch from 1752ba0 to ae51161 Compare December 2, 2017 15:13
@nicolas-grekas
Copy link
Member

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';
Copy link
Member

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.
Copy link
Member

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.

Copy link
Member

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.

@xabbuh
Copy link
Member

xabbuh commented Dec 11, 2017

The deprecation also needs to be documented in UPGRADE-4.1.md, UPGRADE-5.0.md and the component's changelog file. But otherwise this looks good to me.

@PurpleBooth
Copy link
Contributor Author

I'll update those tonight, or at lunchtime if I get a chance 👍

@PurpleBooth PurpleBooth force-pushed the optional-email-filter branch 2 times, most recently from 879b4b4 to 9d5f021 Compare December 11, 2017 19:27
@PurpleBooth
Copy link
Contributor Author

That should be the version numbers corrected in the deprication notices

@PurpleBooth PurpleBooth force-pushed the optional-email-filter branch from 9d5f021 to 7796364 Compare December 11, 2017 19:38
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'`
@PurpleBooth PurpleBooth force-pushed the optional-email-filter branch from 7796364 to cf04108 Compare December 11, 2017 19:49
Copy link
Member

@fabpot fabpot left a 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!

@fabpot
Copy link
Member

fabpot commented Dec 11, 2017

Thank you @PurpleBooth.

@fabpot fabpot merged commit cf04108 into symfony:master Dec 11, 2017
fabpot added a commit that referenced this pull request Dec 11, 2017
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
@PurpleBooth PurpleBooth deleted the optional-email-filter branch December 11, 2017 20:53
xabbuh added a commit to symfony/symfony-docs that referenced this pull request Dec 22, 2017
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
@fabpot fabpot mentioned this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants