Skip to content

Commit 64e720a

Browse files
committed
Merge pull request symfony#2053 from l3l0/add-some-more-missing-xml-constraints-reference
[Reference][Constraints]Added missing xml configuration examples for validation constraints
2 parents 11a4e97 + ac44303 commit 64e720a

17 files changed

+184
-8
lines changed

reference/constraints/All.rst

+16
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ entry in that array:
5151
protected $favoriteColors = array();
5252
}
5353
54+
.. code-block:: xml
55+
56+
<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
57+
<class name="Acme\UserBundle\Entity\User">
58+
<property name="favoriteColors">
59+
<constraint name="All">
60+
<option name="constraints">
61+
<constraint name="NotBlank" />
62+
<constraint name="Length">
63+
<option name="min">5</option>
64+
</constraint>
65+
</option>
66+
</constraint>
67+
</property>
68+
</class>
69+
5470
Now, each entry in the ``favoriteColors`` array will be validated to not
5571
be blank and to be at least 5 characters long.
5672

reference/constraints/Choice.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ If your valid choice list is simple, you can pass them in directly via the
4949
.. code-block:: xml
5050
5151
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
52-
<class name="Acme\BlogBundle\EntityAuthor">
52+
<class name="Acme\BlogBundle\Entity\Author">
5353
<property name="gender">
5454
<constraint name="Choice">
5555
<option name="choices">
@@ -280,4 +280,4 @@ strict
280280

281281
If true, the validator will also check the type of the input value. Specifically,
282282
this value is passed to as the third argument to the PHP :phpfunction:`in_array` method
283-
when checking to see if a value is in the valid choices array.
283+
when checking to see if a value is in the valid choices array.

reference/constraints/Count.rst

+18-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ you might add the following:
3838
- Count:
3939
min: 1
4040
max: 5
41-
minMessage: You must specify at least one email
42-
maxMessage: You cannot specify more than 5 emails
41+
minMessage: "You must specify at least one email"
42+
maxMessage: "You cannot specify more than {{ limit }} emails"
4343
4444
.. code-block:: php-annotations
4545
@@ -53,12 +53,27 @@ you might add the following:
5353
* min = "1",
5454
* max = "5",
5555
* minMessage = "You must specify at least one email",
56-
* maxMessage = "You cannot specify more than 5 emails"
56+
* maxMessage = "You cannot specify more than {{ limit }} emails"
5757
* )
5858
*/
5959
protected $emails = array();
6060
}
6161
62+
.. code-block:: xml
63+
64+
<!-- src/Acme/EventBundle/Resources/config/validation.xml -->
65+
<class name="Acme\EventBundle\Entity\Participant">
66+
<property name="emails">
67+
<constraint name="Count">
68+
<option name="min">1</option>
69+
<option name="max">5</option>
70+
<option name="minMessage">You must specify at least one email</option>
71+
<option name="maxMessage">You cannot specify more than {{ limit }} emails</option>
72+
</constraint>
73+
</property>
74+
</class>
75+
76+
6277
Options
6378
-------
6479

reference/constraints/Country.rst

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ Basic Usage
4141
protected $country;
4242
}
4343
44+
.. code-block:: xml
45+
46+
<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
47+
<class name="Acme\UserBundle\Entity\User">
48+
<property name="country">
49+
<constraint name="Country" />
50+
</property>
51+
</class>
52+
4453
Options
4554
-------
4655

reference/constraints/Date.rst

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ Basic Usage
4141
protected $birthday;
4242
}
4343
44+
.. code-block:: xml
45+
46+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
47+
<class name="Acme\BlogBundle\Entity\Author">
48+
<property name="birthday">
49+
<constraint name="Date" />
50+
</property>
51+
</class>
52+
4453
Options
4554
-------
4655

reference/constraints/DateTime.rst

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ Basic Usage
4343
protected $createdAt;
4444
}
4545
46+
.. code-block:: xml
47+
48+
<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
49+
<class name="Acme\BlogBundle\Entity\Author">
50+
<property name="createdAt">
51+
<constraint name="DateTime" />
52+
</property>
53+
</class>
54+
4655
Options
4756
-------
4857

reference/constraints/False.rst

+11
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ method returns **false**:
4848
- "False":
4949
message: You've entered an invalid state.
5050
51+
.. code-block:: xml
52+
53+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
54+
<class name="Acme\BlogBundle\Entity\Author">
55+
<getter property="stateInvalid">
56+
<constraint name="False">
57+
<option name="message">You've entered an invalid state.</option>
58+
</constraint>
59+
</getter>
60+
</class>
61+
5162
.. code-block:: php-annotations
5263
5364
// src/Acme/BlogBundle/Entity/Author.php

reference/constraints/Ip.rst

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ Basic Usage
4444
protected $ipAddress;
4545
}
4646
47+
.. code-block:: xml
48+
49+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
50+
<class name="Acme\BlogBundle\Entity\Author">
51+
<property name="ipAddress">
52+
<constraint name="Ip" />
53+
</property>
54+
</class>
55+
4756
Options
4857
-------
4958

reference/constraints/Language.rst

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ Basic Usage
4141
protected $preferredLanguage;
4242
}
4343
44+
.. code-block:: xml
45+
46+
<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
47+
<class name="Acme\UserBundle\Entity\User">
48+
<property name="preferredLanguage">
49+
<constraint name="Language" />
50+
</property>
51+
</class>
52+
4453
Options
4554
-------
4655

reference/constraints/Length.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ To verify that the ``firstName`` field length of a class is between "2" and
6161
6262
.. code-block:: xml
6363
64-
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
64+
<!-- src/Acme/EventBundle/Resources/config/validation.xml -->
6565
<class name="Acme\EventBundle\Entity\Participant">
6666
<property name="firstName">
6767
<constraint name="Length">

reference/constraints/Locale.rst

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ Basic Usage
4545
protected $locale;
4646
}
4747
48+
.. code-block:: xml
49+
50+
<!-- src/Acme/UserBundle/Resources/config/validation.xml -->
51+
<class name="Acme\UserBundle\Entity\User">
52+
<property name="locale">
53+
<constraint name="Locale" />
54+
</property>
55+
</class>
56+
4857
Options
4958
-------
5059

reference/constraints/Max.rst

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ add the following:
5050
protected $age;
5151
}
5252
53+
.. code-block:: xml
54+
55+
<!-- src/Acme/EventBundle/Resources/config/validation.yml -->
56+
<class name="Acme\EventBundle\Entity\Participant">
57+
<property name="age">
58+
<constraint name="Max">
59+
<option name="limit">50</option>
60+
<option name="message">You must be 50 or under to enter.</option>
61+
</constraint>
62+
</property>
63+
</class>
64+
5365
Options
5466
-------
5567

reference/constraints/Range.rst

+14
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ the following:
5858
protected $height;
5959
}
6060
61+
.. code-block:: xml
62+
63+
<!-- src/Acme/EventBundle/Resources/config/validation.xml -->
64+
<class name="Acme\EventBundle\Entity\Participant">
65+
<property name="height">
66+
<constraint name="Range">
67+
<option name="min">120</option>
68+
<option name="max">180</option>
69+
<option name="minMessage">You must be at least 120cm tall to enter</option>
70+
<option name="maxMessage">You cannot be taller than 180cm to enter</option>
71+
</constraint>
72+
</property>
73+
</class>
74+
6175
Options
6276
-------
6377

reference/constraints/Regex.rst

+25-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ characters at the beginning of your string:
4848
protected $description;
4949
}
5050
51+
.. code-block:: xml
52+
53+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
54+
<class name="Acme\BlogBundle\Entity\Author">
55+
<property name="description">
56+
<constraint name="Regex">
57+
<option name="pattern">/^\w+/</option>
58+
</constraint>
59+
</property>
60+
</class>
61+
5162
Alternatively, you can set the `match`_ option to ``false`` in order to assert
5263
that a given string does *not* match. In the following example, you'll assert
5364
that the ``firstName`` field does not contain any numbers and give it a custom
@@ -85,6 +96,19 @@ message:
8596
protected $firstName;
8697
}
8798
99+
.. code-block:: xml
100+
101+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
102+
<class name="Acme\BlogBundle\Entity\Author">
103+
<property name="firstName">
104+
<constraint name="Regex">
105+
<option name="pattern">/\d/</option>
106+
<option name="match">false</option>
107+
<option name="message">Your name cannot contain a number</option>
108+
</constraint>
109+
</property>
110+
</class>
111+
88112
Options
89113
-------
90114

@@ -114,4 +138,4 @@ message
114138

115139
**type**: ``string`` **default**: ``This value is not valid``
116140

117-
This is the message that will be shown if this validator fails.
141+
This is the message that will be shown if this validator fails.

reference/constraints/Time.rst

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ of the day when the event starts:
4646
protected $startsAt;
4747
}
4848
49+
.. code-block:: xml
50+
51+
<!-- src/Acme/EventBundle/Resources/config/validation.xml -->
52+
<class name="Acme\EventBundle\Entity\Event">
53+
<property name="startsAt">
54+
<constraint name="Time" />
55+
</property>
56+
</class>
57+
4958
Options
5059
-------
5160

reference/constraints/Type.rst

+13-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ Basic Usage
4646
protected $age;
4747
}
4848
49+
.. code-block:: xml
50+
51+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
52+
<class name="Acme\BlogBundle\Entity\Author">
53+
<property name="age">
54+
<constraint name="Type">
55+
<option name="type">integer</option>
56+
<option name="message">The value {{ value }} is not a valid {{ type }}.</option>
57+
</constraint>
58+
</property>
59+
</class>
60+
4961
Options
5062
-------
5163

@@ -80,4 +92,4 @@ message
8092

8193
**type**: ``string`` **default**: ``This value should be of type {{ type }}``
8294

83-
The message if the underlying data is not of the given type.
95+
The message if the underlying data is not of the given type.

reference/constraints/Url.rst

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ Basic Usage
4242
protected $bioUrl;
4343
}
4444
45+
.. code-block:: xml
46+
47+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
48+
<class name="Acme\BlogBundle\Entity\Author">
49+
<property name="bioUrl">
50+
<constraint name="Url" />
51+
</property>
52+
</class>
53+
4554
Options
4655
-------
4756

0 commit comments

Comments
 (0)