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][Email] - Strict validation and soft dependency #9140
Changes from 14 commits
885996b
b377295
c3241ec
ab3787a
fb36789
6574d5f
1052f3b
33cb5c5
facef8e
7ce2528
58e99b5
fadea1f
5791e36
a7ec6ab
89ca713
cf11a03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -655,6 +655,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder | |
$container->setParameter('validator.translation_domain', $config['translation_domain']); | ||
$container->setParameter('validator.mapping.loader.xml_files_loader.mapping_files', $this->getValidatorXmlMappingFiles($container)); | ||
$container->setParameter('validator.mapping.loader.yaml_files_loader.mapping_files', $this->getValidatorYamlMappingFiles($container)); | ||
$container->setParameter('validator.strict_email', $config['strict_email']); | ||
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. We try to avoid creating new parameters as much as possible. Instead, can you inject the value into the service? Something like this (not tested): $definition = $container->findDefinition('validator.email');
$definition->replaceArgument(0, $config['strict_email']); |
||
|
||
if (array_key_exists('enable_annotations', $config) && $config['enable_annotations']) { | ||
$loaderChain = $container->getDefinition('validator.mapping.loader.loader_chain'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
<parameter key="validator.mapping.loader.xml_files_loader.mapping_files" type="collection" /> | ||
<parameter key="validator.mapping.loader.yaml_files_loader.mapping_files" type="collection" /> | ||
<parameter key="validator.expression.class">Symfony\Component\Validator\Constraints\ExpressionValidator</parameter> | ||
<parameter key="validator.email.class">Symfony\Component\Validator\Constraints\EmailValidator</parameter> | ||
</parameters> | ||
|
||
<services> | ||
|
@@ -69,5 +70,10 @@ | |
<argument type="service" id="property_accessor" /> | ||
<tag name="validator.constraint_validator" alias="validator.expression" /> | ||
</service> | ||
|
||
<service id="validator.email" class="%validator.email.class%"> | ||
<argument>%validator.strict_email%</argument> | ||
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. This should be replaced with an empty value (see above.) |
||
<tag name="validator.constraint_validator" alias="validator.email" /> | ||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ CHANGELOG | |
|
||
* deprecated `ApcCache` in favor of `DoctrineCache` | ||
* added `DoctrineCache` to adapt any Doctrine cache | ||
* [BC BREAK] `EmailValidator` now has a lighter validation by default, uses `Egulias\EmailValidator` for strict validation | ||
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. I don't think that this is a BC break. 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. I thought it was because some emails will pass with the non-strict validation. I'll remove this then. |
||
|
||
2.4.0 | ||
----- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
use Egulias\EmailValidator\EmailValidator as StrictEmailValidator; | ||
|
||
/** | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
|
@@ -22,6 +23,18 @@ | |
*/ | ||
class EmailValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* isStrict | ||
* | ||
* @var Boolean | ||
*/ | ||
private $isStrict; | ||
|
||
public function __construct($strict) | ||
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. missing phpdoc. and it should set a default as well to not break BC. 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. Because of the following:
|
||
{ | ||
$this->isStrict = $strict; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
|
@@ -36,12 +49,23 @@ public function validate($value, Constraint $constraint) | |
} | ||
|
||
$value = (string) $value; | ||
$valid = filter_var($value, FILTER_VALIDATE_EMAIL); | ||
if ($constraint->strict === null) { | ||
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. null === $constraint->strict |
||
$constraint->strict = $this->isStrict; | ||
} | ||
|
||
if (true === $constraint->strict && class_exists('\Egulias\EmailValidator\EmailValidator')) { | ||
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.
|
||
$strictValidator = new StrictEmailValidator(); | ||
$valid = $strictValidator->isValid($value, $constraint->checkMX); | ||
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. You should probably disable MX checks here as it's going to be redone below anyway. |
||
} elseif ($constraint->strict === true) { | ||
throw new \RuntimeException('Strict email validation requires egulias/email-validator'); | ||
} else { | ||
$valid = preg_match('/.+\@.+\..+/', $value); | ||
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. Why is 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. Yes it does, that's why I was saying this is still a BC break. But I did it this way because of the conversation on #1581 where, if I didn't got it wrong, 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. you mean BC break. BC means the opposite 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. Yes BC break, sorry.
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. @jakzal it's ok? Before I fix the other changes. 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. Fine with me if it was agreed before :) |
||
} | ||
|
||
if ($valid) { | ||
$host = substr($value, strpos($value, '@') + 1); | ||
|
||
// Check for host DNS resource records | ||
|
||
if ($valid && $constraint->checkMX) { | ||
$valid = $this->checkMX($host); | ||
} elseif ($valid && $constraint->checkHost) { | ||
|
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 also don't see an entry on the changelog
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.
See L9
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.
changelog not upgrade i mean
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.
There is no changelog file in master yet, how should I proceed?
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 are talking about the changelog of the component: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/CHANGELOG.md