Skip to content

Commit ad2f906

Browse files
committed
[Validator] Added a check DNS option on URL validator
1 parent 193d69c commit ad2f906

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/Symfony/Component/Validator/Constraints/Url.php

+2
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@
2424
class Url extends Constraint
2525
{
2626
public $message = 'This value is not a valid URL.';
27+
public $dnsMessage = 'The host could not be resolved.';
2728
public $protocols = array('http', 'https');
29+
public $checkDNS = false;
2830
}

src/Symfony/Component/Validator/Constraints/UrlValidator.php

+12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ public function validate($value, Constraint $constraint)
6262
$this->buildViolation($constraint->message)
6363
->setParameter('{{ value }}', $this->formatValue($value))
6464
->addViolation();
65+
66+
return;
67+
}
68+
69+
if ($constraint->checkDNS) {
70+
$host = parse_url($value, PHP_URL_HOST);
71+
72+
if (!checkdnsrr($host, 'ANY')) {
73+
$this->buildViolation($constraint->dnsMessage)
74+
->setParameter('{{ value }}', $this->formatValue($host))
75+
->addViolation();
76+
}
6577
}
6678
}
6779
}

src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

+4
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@
302302
<source>An empty file is not allowed.</source>
303303
<target>An empty file is not allowed.</target>
304304
</trans-unit>
305+
<trans-unit id="79">
306+
<source>The host could not be resolved.</source>
307+
<target>The host could not be resolved.</target>
308+
</trans-unit>
305309
</body>
306310
</file>
307311
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf

+4
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@
302302
<source>An empty file is not allowed.</source>
303303
<target>Un fichier vide n'est pas autorisé.</target>
304304
</trans-unit>
305+
<trans-unit id="79">
306+
<source>The host could not be resolved.</source>
307+
<target>Le nom de domaine n'a pas pu être résolu.</target>
308+
</trans-unit>
305309
</body>
306310
</file>
307311
</xliff>

0 commit comments

Comments
 (0)