[Validator] Added a check DNS option on URL validator

This commit is contained in:
Saro0h 2014-12-11 23:56:13 +01:00
parent 193d69c4d1
commit ad2f906daf
4 changed files with 22 additions and 0 deletions

View File

@ -24,5 +24,7 @@ use Symfony\Component\Validator\Constraint;
class Url extends Constraint
{
public $message = 'This value is not a valid URL.';
public $dnsMessage = 'The host could not be resolved.';
public $protocols = array('http', 'https');
public $checkDNS = false;
}

View File

@ -62,6 +62,18 @@ class UrlValidator extends ConstraintValidator
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->addViolation();
return;
}
if ($constraint->checkDNS) {
$host = parse_url($value, PHP_URL_HOST);
if (!checkdnsrr($host, 'ANY')) {
$this->buildViolation($constraint->dnsMessage)
->setParameter('{{ value }}', $this->formatValue($host))
->addViolation();
}
}
}
}

View File

@ -302,6 +302,10 @@
<source>An empty file is not allowed.</source>
<target>An empty file is not allowed.</target>
</trans-unit>
<trans-unit id="79">
<source>The host could not be resolved.</source>
<target>The host could not be resolved.</target>
</trans-unit>
</body>
</file>
</xliff>

View File

@ -302,6 +302,10 @@
<source>An empty file is not allowed.</source>
<target>Un fichier vide n'est pas autorisé.</target>
</trans-unit>
<trans-unit id="79">
<source>The host could not be resolved.</source>
<target>Le nom de domaine n'a pas pu être résolu.</target>
</trans-unit>
</body>
</file>
</xliff>