Skip to content

Commit 07628c9

Browse files
javiereguiluzweaverryan
authored andcommitted
Simplified the first example and added more examples
1 parent 3395e02 commit 07628c9

File tree

1 file changed

+135
-19
lines changed

1 file changed

+135
-19
lines changed

reference/constraints/Url.rst

Lines changed: 135 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,72 @@ Validates that a value is a valid URL string.
1717
Basic Usage
1818
-----------
1919

20+
.. configuration-block::
21+
22+
.. code-block:: yaml
23+
24+
# src/Acme/BlogBundle/Resources/config/validation.yml
25+
Acme\BlogBundle\Entity\Author:
26+
properties:
27+
bioUrl:
28+
- Url: ~
29+
30+
.. code-block:: php-annotations
31+
32+
// src/Acme/BlogBundle/Entity/Author.php
33+
namespace Acme\BlogBundle\Entity;
34+
35+
use Symfony\Component\Validator\Constraints as Assert;
36+
37+
class Author
38+
{
39+
/**
40+
* @Assert\Url()
41+
*/
42+
protected $bioUrl;
43+
}
44+
45+
.. code-block:: xml
46+
47+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
48+
<?xml version="1.0" encoding="UTF-8" ?>
49+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
50+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
51+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
52+
53+
<class name="Acme\BlogBundle\Entity\Author">
54+
<property name="bioUrl">
55+
<constraint name="Url" />
56+
</property>
57+
</class>
58+
</constraint-mapping>
59+
60+
.. code-block:: php
61+
62+
// src/Acme/BlogBundle/Entity/Author.php
63+
namespace Acme\BlogBundle\Entity;
64+
65+
use Symfony\Component\Validator\Mapping\ClassMetadata;
66+
use Symfony\Component\Validator\Constraints as Assert;
67+
68+
class Author
69+
{
70+
public static function loadValidatorMetadata(ClassMetadata $metadata)
71+
{
72+
$metadata->addPropertyConstraint('bioUrl', new Assert\Url());
73+
}
74+
}
75+
76+
Options
77+
-------
78+
79+
message
80+
~~~~~~~
81+
82+
**type**: ``string`` **default**: ``This value is not a valid URL.``
83+
84+
This message is shown if the URL is invalid.
85+
2086
.. configuration-block::
2187

2288
.. code-block:: yaml
@@ -27,7 +93,6 @@ Basic Usage
2793
bioUrl:
2894
- Url: ~
2995
message: The url "{{ value }}" is not a valid url.
30-
protocols: [http, https]
3196
3297
.. code-block:: php-annotations
3398
@@ -41,7 +106,6 @@ Basic Usage
41106
/**
42107
* @Assert\Url(
43108
* message = "The url '{{ value }}' is not a valid url",
44-
* protocols = {"http", "https"}
45109
* )
46110
*/
47111
protected $bioUrl;
@@ -59,10 +123,6 @@ Basic Usage
59123
<property name="bioUrl">
60124
<constraint name="Url">
61125
<option name="message">The url "{{ value }}" is not a valid url.</option>
62-
<option name="protocols">
63-
<value>http</value>
64-
<value>https</value>
65-
</option>
66126
</constraint>
67127
</property>
68128
</class>
@@ -82,26 +142,82 @@ Basic Usage
82142
{
83143
$metadata->addPropertyConstraint('bioUrl', new Assert\Url(array(
84144
'message' => 'The url "{{ value }}" is not a valid url.',
85-
'protocols' => array('http', 'https'),
86145
)));
87146
}
88147
}
89148
90-
Options
91-
-------
149+
protocols
150+
~~~~~~~~~
92151

93-
message
94-
~~~~~~~
152+
**type**: ``array`` **default**: ``array('http', 'https')``
95153

96-
**type**: ``string`` **default**: ``This value is not a valid URL.``
154+
The protocols considered to be valid for the URL. For example, if you also consider
155+
the ``ftp://`` type URLs to be valid, redefine the ``protocols`` array, listing
156+
``http``, ``https``, and also ``ftp``.
97157

98-
This message is shown if the URL is invalid.
158+
.. configuration-block::
99159

100-
protocols
101-
~~~~~~~~~
160+
.. code-block:: yaml
102161
103-
**type**: ``array`` **default**: ``array('http', 'https')``
162+
# src/Acme/BlogBundle/Resources/config/validation.yml
163+
Acme\BlogBundle\Entity\Author:
164+
properties:
165+
bioUrl:
166+
- Url: ~
167+
protocols: [http, https, ftp]
168+
169+
.. code-block:: php-annotations
170+
171+
// src/Acme/BlogBundle/Entity/Author.php
172+
namespace Acme\BlogBundle\Entity;
173+
174+
use Symfony\Component\Validator\Constraints as Assert;
175+
176+
class Author
177+
{
178+
/**
179+
* @Assert\Url(
180+
* protocols = {"http", "https", "ftp"}
181+
* )
182+
*/
183+
protected $bioUrl;
184+
}
185+
186+
.. code-block:: xml
104187
105-
The protocols that will be considered to be valid. For example, if you also
106-
needed ``ftp://`` type URLs to be valid, you'd redefine the ``protocols``
107-
array, listing ``http``, ``https`` and also ``ftp``.
188+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
189+
<?xml version="1.0" encoding="UTF-8" ?>
190+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
191+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
192+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
193+
194+
<class name="Acme\BlogBundle\Entity\Author">
195+
<property name="bioUrl">
196+
<constraint name="Url">
197+
<option name="protocols">
198+
<value>http</value>
199+
<value>https</value>
200+
<value>ftp</value>
201+
</option>
202+
</constraint>
203+
</property>
204+
</class>
205+
</constraint-mapping>
206+
207+
.. code-block:: php
208+
209+
// src/Acme/BlogBundle/Entity/Author.php
210+
namespace Acme\BlogBundle\Entity;
211+
212+
use Symfony\Component\Validator\Mapping\ClassMetadata;
213+
use Symfony\Component\Validator\Constraints as Assert;
214+
215+
class Author
216+
{
217+
public static function loadValidatorMetadata(ClassMetadata $metadata)
218+
{
219+
$metadata->addPropertyConstraint('bioUrl', new Assert\Url(array(
220+
'protocols' => array('http', 'https', 'ftp'),
221+
)));
222+
}
223+
}

0 commit comments

Comments
 (0)