@@ -8,7 +8,7 @@ Validates that a value is a valid URL string.
8
8
+----------------+---------------------------------------------------------------------+
9
9
| Options | - `message `_ |
10
10
| | - `protocols `_ |
11
- | | - `payload `_ |
11
+ | | - `checkDNS `_ |
12
12
+----------------+---------------------------------------------------------------------+
13
13
| Class | :class: `Symfony\\ Component\\ Validator\\ Constraints\\ Url ` |
14
14
+----------------+---------------------------------------------------------------------+
@@ -20,6 +20,16 @@ Basic Usage
20
20
21
21
.. configuration-block ::
22
22
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
+
23
33
.. code-block :: php-annotations
24
34
25
35
// src/Acme/BlogBundle/Entity/Author.php
@@ -30,19 +40,14 @@ Basic Usage
30
40
class Author
31
41
{
32
42
/**
33
- * @Assert\Url()
43
+ * @Assert\Url(
44
+ * message = "The url '{{ value }}' is not a valid url",
45
+ * protocols = {"http", "https"}
46
+ * )
34
47
*/
35
48
protected $bioUrl;
36
49
}
37
50
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
-
46
51
.. code-block :: xml
47
52
48
53
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
@@ -53,7 +58,13 @@ Basic Usage
53
58
54
59
<class name =" Acme\BlogBundle\Entity\Author" >
55
60
<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 >
57
68
</property >
58
69
</class >
59
70
</constraint-mapping >
@@ -70,7 +81,10 @@ Basic Usage
70
81
{
71
82
public static function loadValidatorMetadata(ClassMetadata $metadata)
72
83
{
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
+ )));
74
88
}
75
89
}
76
90
@@ -91,6 +105,90 @@ protocols
91
105
92
106
The protocols that will be considered to be valid. For example, if you also
93
107
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
+ }
95
192
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