8000 [Validator, FrameworkBundle] Add prevent DNS lookup option to EmailValidator by patrick-mcdougle · Pull Request #18142 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator, FrameworkBundle] Add prevent DNS lookup option to EmailValidator #18142

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
->end()
->scalarNode('translation_domain')->defaultValue('validators')->end()
->booleanNode('strict_email')->defaultFalse()->end()
->booleanNode('use_dns')->defaultTrue()->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder

$definition = $container->findDefinition('validator.email');
$definition->replaceArgument(0, $config['strict_email']);
$definition->replaceArgument(1, $config['use_dns']);

if (array_key_exists('enable_annotations', $config) && $config['enable_annotations']) {
$validatorBuilder->addMethodCall('enableAnnotationMapping', array(new Reference('annotation_reader')));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
</service>

<service id="validator.email" class="Symfony\Component\Validator\Constraints\EmailValidator">
<argument></argument>
<argument></argument>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good IMO to add comment that those two options are passed in extension.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way we do this elsewhere is like this: <argument /> <!-- XXX -->

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will do later tonight.

<tag name="validator.constraint_validator" alias="Symfony\Component\Validator\Constraints\EmailValidator" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ protected static function getBundleDefaultConfig()
'static_method' => array('loadValidatorMetadata'),
'translation_domain' => 'validators',
'strict_email' => false,
'use_dns' => true,
),
'annotations' => array(
'cache' => 'file',
Expand Down
36 changes: 22 additions & 14 deletions src/Symfony/Component/Validator/Constraints/EmailValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ class EmailValidator extends ConstraintValidator
*/
private $isStrict;

public function __construct($strict = false)
/**
* @var bool
*/
private $useDNS;

public function __construct($strict = false, $useDNS = true)
{
$this->isStrict = $strict;
$this->useDNS = $useDNS;
}

/**
Expand Down Expand Up @@ -80,23 +86,25 @@ public function validate($value, Constraint $constraint)

$host = substr($value, strpos($value, '@') + 1);

// Check for host DNS resource records
if ($constraint->checkMX) {
if (!$this->checkMX($host)) {
if ($this->useDNS) {
// Check for host DNS resource records
if ($constraint->checkMX) {
if (!$this->checkMX($host)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Email::MX_CHECK_FAILED_ERROR)
->addViolation();
}

return;
}

if ($constraint->checkHost && !$this->checkHost($host)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Email::MX_CHECK_FAILED_ERROR)
->setCode(Email::HOST_CHECK_FAILED_ERROR)
->addViolation();
}

return;
}

if ($constraint->checkHost && !$this->checkHost($host)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Email::HOST_CHECK_FAILED_ERROR)
->addViolation();
}
}

Expand Down
0