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
Changes from 1 commit
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
Next Next commit
Introduce flag to prevent DNS lookups
  • Loading branch information
patrick-mcdougle committed Mar 12, 2016
commit e963fc027a05a4ce3660b395b22d74738aaf40ce
12 changes: 9 additions & 3 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 $preventDNSLookups;

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

/**
Expand Down Expand Up @@ -81,7 +87,7 @@ public function validate($value, Constraint $constraint)
$host = substr($value, strpos($value, '@') + 1);

// Check for host DNS resource records
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a little more DRY if there were just a single if (!$this->preventDNSLookups) conditional wrapping both the MX section and the hostname section. It would also keep the original if statements uncluttered by this new logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great suggestion! I'll make this change when I get back to my computer.

if ($constraint->checkMX) {
if (!$this->preventDNSLookups && $constraint->checkMX) {
if (!$this->checkMX($host)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
Expand All @@ -92,7 +98,7 @@ public function validate($value, Constraint $constraint)
return;
}

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