8000 feature #12956 [2.7][Validator] Added checkDNS option on URL validato… · symfony/validator@2d4a087 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d4a087

Browse files
committed
feature #12956 [2.7][Validator] Added checkDNS option on URL validator (saro0h)
This PR was merged into the 2.7 branch. Discussion ---------- [2.7][Validator] Added checkDNS option on URL validator | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #10088 | License | MIT | Doc PR | symfony/symfony-docs#4631 Commits ------- ad2f906 [Validator] Added a check DNS option on URL validator
2 parents c1cf527 + 6b63723 commit 2d4a087

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

Constraints/Url.php

Lines changed: 2 additions & 0 deletions
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
}

Constraints/UrlValidator.php

Lines changed: 12 additions & 0 deletions
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
}

Resources/translations/validators.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line nu 8000 mberDiff 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>

Resources/translations/validators.fr.xlf

Lines changed: 4 additions & 0 deletions
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)
0