Skip to content

Commit 75da2f3

Browse files
saro0hjaviereguiluz
authored andcommitted
[Validator] Updated documentation of URL validator
Updated the documentation regarding the Pull Request on Symfony : symfony/symfony#12956
1 parent 9fd5229 commit 75da2f3

File tree

1 file changed

+112
-14
lines changed

1 file changed

+112
-14
lines changed

Diff for: reference/constraints/Url.rst

+112-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Validates that a value is a valid URL string.
88
+----------------+---------------------------------------------------------------------+
99
| Options | - `message`_ |
1010
| | - `protocols`_ |
11-
| | - `payload`_ |
11+
| | - `checkDNS`_ |
1212
+----------------+---------------------------------------------------------------------+
1313
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Url` |
1414
+----------------+---------------------------------------------------------------------+
@@ -20,6 +20,16 @@ Basic Usage
2020

2121
.. configuration-block::
2222

23+
.. code-block:: yaml
24+
25+
# src/Acme/BlogBundle/Resources/config/validation.yml
26+
Acme\BlogBundle\Entity\Author:
27+
properties:
28+
bioUrl:
29+
- Url: ~
30+
message: The url "{{ value }}" is not a valid url.
31+
protocols: [http, https]
32+
2333
.. code-block:: php-annotations
2434
2535
// src/Acme/BlogBundle/Entity/Author.php
@@ -30,19 +40,14 @@ Basic Usage
3040
class Author
3141
{
3242
/**
33-
* @Assert\Url()
43+
* @Assert\Url(
44+
* message = "The url '{{ value }}' is not a valid url",
45+
* protocols = {"http", "https"}
46+
* )
3447
*/
3548
protected $bioUrl;
3649
}
3750
38-
.. code-block:: yaml
39-
40-
# src/Acme/BlogBundle/Resources/config/validation.yml
41-
Acme\BlogBundle\Entity\Author:
42-
properties:
43-
bioUrl:
44-
- Url: ~
45-
4651
.. code-block:: xml
4752
4853
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
@@ -53,7 +58,13 @@ Basic Usage
5358
5459
<class name="Acme\BlogBundle\Entity\Author">
5560
<property name="bioUrl">
56-
<constraint name="Url" />
61+
<constraint name="Url">
62+
<option name="message">The url "{{ value }}" is not a valid url.</option>
63+
<option name="protocols">
64+
<value>http</value>
65+
<value>https</value>
66+
</option>
67+
</constraint>
5768
</property>
5869
</class>
5970
</constraint-mapping>
@@ -70,7 +81,10 @@ Basic Usage
7081
{
7182
public static function loadValidatorMetadata(ClassMetadata $metadata)
7283
{
73-
$metadata->addPropertyConstraint('bioUrl', new Assert\Url());
84+
$metadata->addPropertyConstraint('bioUrl', new Assert\Url(array(
85+
'message' => 'The url "{{ value }}" is not a valid url.',
86+
'protocols' => array('http', 'https'),
87+
)));
7488
}
7589
}
7690
@@ -91,6 +105,90 @@ protocols
91105

92106
The protocols that will be considered to be valid. For example, if you also
93107
needed ``ftp://`` type URLs to be valid, you'd redefine the ``protocols``
94-
array, listing ``http``, ``https`` and also ``ftp``.
108+
array, listing ``http``, ``https``, and also ``ftp``.
109+
110+
checkDNS
111+
~~~~~~~~
112+
113+
**type**: ``Boolean`` **default**: ``false``
114+
115+
By default, this constraint just validates the syntax of the given URL. If you
116+
also need to check whether the associated host exists, set the ``checkDNS``
117+
option to ``true``:
118+
119+
.. configuration-block::
120+
121+
.. code-block:: yaml
122+
123+
# src/Acme/BlogBundle/Resources/config/validation.yml
124+
Acme\BlogBundle\Entity\Author:
125+
properties:
126+
bioUrl:
127+
- Url: ~
128+
message: The url "{{ value }}" is not a valid url.
129+
protocols: [http, https]
130+
checkDNS: true
131+
132+
.. code-block:: php-annotations
133+
134+
// src/Acme/BlogBundle/Entity/Author.php
135+
namespace Acme\BlogBundle\Entity;
136+
137+
use Symfony\Component\Validator\Constraints as Assert;
138+
139+
class Author
140+
{
141+
/**
142+
* @Assert\Url(
143+
* message = "The url '{{ value }}' is not a valid url",
144+
* protocols = {"http", "https"}
145+
* checkDNS = true
146+
* )
147+
*/
148+
protected $bioUrl;
149+
}
150+
151+
.. code-block:: xml
152+
153+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
154+
<?xml version="1.0" encoding="UTF-8" ?>
155+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
156+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
157+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
158+
159+
<class name="Acme\BlogBundle\Entity\Author">
160+
<property name="bioUrl">
161+
<constraint name="Url">
162+
<option name="message">The url "{{ value }}" is not a valid url.</option>
163+
<option name="protocols">
164+
<value>http</value>
165+
<value>https</value>
166+
</option>
167+
<option name="checkDNS">true</option>
168+
</constraint>
169+
</property>
170+
</class>
171+
</constraint-mapping>
172+
173+
.. code-block:: php
174+
175+
// src/Acme/BlogBundle/Entity/Author.php
176+
namespace Acme\BlogBundle\Entity;
177+
178+
use Symfony\Component\Validator\Mapping\ClassMetadata;
179+
use Symfony\Component\Validator\Constraints as Assert;
180+
181+
class Author
182+
{
183+
public static function loadValidatorMetadata(ClassMetadata $metadata)
184+
{
185+
$metadata->addPropertyConstraint('bioUrl', new Assert\Url(array(
186+
'message' => 'The url "{{ value }}" is not a valid url.',
187+
'protocols' => array('http', 'https'),
188+
'checkDNS' => true,
189+
)));
190+
}
191+
}
95192
96-
.. include:: /reference/constraints/_payload-option.rst.inc
193+
This option uses the :phpfunction:`checkdnsrr` PHP function to check the validity
194+
of the ``ANY`` DNS record corresponding to the host associated with the given URL.

0 commit comments

Comments
 (0)