Skip to content

Commit 9ef9117

Browse files
committed
Merge pull request symfony#2247 from WouterJ/reorderd_config_blocks
[Constraints] Fixed incosistenties and reordered config blocks
2 parents 672ded5 + 5f4e6ef commit 9ef9117

File tree

8 files changed

+207
-121
lines changed

8 files changed

+207
-121
lines changed

reference/constraints/Callback.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ process. Each method can be one of the following formats:
158158
{
159159
}
160160
161+
.. code-block:: xml
162+
163+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
164+
<class name="Acme\BlogBundle\Entity\Author">
165+
<constraint name="Callback">
166+
<option name="methods">
167+
<value>Acme\BlogBundle\MyStaticValidatorClass</value>
168+
<value>isAuthorValid</value>
169+
</option>
170+
</constraint>
171+
</class>
172+
161173
.. code-block:: php
162174
163175
// src/Acme/BlogBundle/Entity/Author.php

reference/constraints/Choice.rst

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,6 @@ If your valid choice list is simple, you can pass them in directly via the
4646
choices: [male, female]
4747
message: Choose a valid gender.
4848
49-
.. code-block:: xml
50-
51-
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
52-
<class name="Acme\BlogBundle\Entity\Author">
53-
<property name="gender">
54-
<constraint name="Choice">
55-
<option name="choices">
56-
<value>male</value>
57-
<value>female</value>
58-
</option>
59-
<option name="message">Choose a valid gender.</option>
60-
</constraint>
61-
</property>
62-
</class>
63-
6449
.. code-block:: php-annotations
6550
6651
// src/Acme/BlogBundle/Entity/Author.php
@@ -76,6 +61,21 @@ If your valid choice list is simple, you can pass them in directly via the
7661
protected $gender;
7762
}
7863
64+
.. code-block:: xml
65+
66+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
67+
<class name="Acme\BlogBundle\Entity\Author">
68+
<property name="gender">
69+
<constraint name="Choice">
70+
<option name="choices">
71+
<value>male</value>
72+
<value>female</value>
73+
</option>
74+
<option name="message">Choose a valid gender.</option>
75+
</constraint>
76+
</property>
77+
</class>
78+
7979
.. code-block:: php
8080
8181
// src/Acme/BlogBundle/EntityAuthor.php
@@ -108,6 +108,8 @@ form element.
108108
.. code-block:: php
109109
110110
// src/Acme/BlogBundle/Entity/Author.php
111+
namespace Acme\BlogBundle\Entity;
112+
111113
class Author
112114
{
113115
public static function getGenders()
@@ -132,6 +134,8 @@ constraint.
132134
.. code-block:: php-annotations
133135
134136
// src/Acme/BlogBundle/Entity/Author.php
137+
namespace Acme\BlogBundle\Entity;
138+
135139
use Symfony\Component\Validator\Constraints as Assert;
136140
137141
class Author
@@ -153,6 +157,26 @@ constraint.
153157
</property>
154158
</class>
155159
160+
.. code-block:: php
161+
162+
// src/Acme/BlogBundle/EntityAuthor.php
163+
namespace Acme\BlogBundle\Entity;
164+
165+
use Symfony\Component\Validator\Mapping\ClassMetadata;
166+
use Symfony\Component\Validator\Constraints as Assert;
167+
168+
class Author
169+
{
170+
protected $gender;
171+
172+
public static function loadValidatorMetadata(ClassMetadata $metadata)
173+
{
174+
$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
175+
'callback' => 'getGenders',
176+
)));
177+
}
178+
}
179+
156180
If the static callback is stored in a different class, for example ``Util``,
157181
you can pass the class name and the method as an array.
158182

@@ -166,6 +190,21 @@ you can pass the class name and the method as an array.
166190
gender:
167191
- Choice: { callback: [Util, getGenders] }
168192
193+
.. code-block:: php-annotations
194+
195+
// src/Acme/BlogBundle/Entity/Author.php
196+
namespace Acme\BlogBundle\Entity;
197+
198+
use Symfony\Component\Validator\Constraints as Assert;
199+
200+
class Author
201+
{
202+
/**
203+
* @Assert\Choice(callback = {"Util", "getGenders"})
204+
*/
205+
protected $gender;
206+
}
207+
169208
.. code-block:: xml
170209
171210
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
@@ -180,17 +219,24 @@ you can pass the class name and the method as an array.
180219
</property>
181220
</class>
182221
183-
.. code-block:: php-annotations
222+
.. code-block:: php
184223
185-
// src/Acme/BlogBundle/Entity/Author.php
186-
use Symfony\Component\Validator\Constraints as Assert;
224+
// src/Acme/BlogBundle/EntityAuthor.php
225+
namespace Acme\BlogBundle\Entity;
187226
227+
use Symfony\Component\Validator\Mapping\ClassMetadata;
228+
use Symfony\Component\Validator\Constraints as Assert;
229+
188230
class Author
189231
{
190-
/**
191-
* @Assert\Choice(callback = {"Util", "getGenders"})
192-
*/
193232
protected $gender;
233+
234+
public static function loadValidatorMetadata(ClassMetadata $metadata)
235+
{
236+
$metadata->addPropertyConstraint('gender', new Assert\Choice(array(
237+
'callback' => array('Util', 'getGenders'),
238+
)));
239+
}
194240
}
195241
196242
Available Options

reference/constraints/Collection.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Basic Usage
3030
The ``Collection`` constraint allows you to validate the different keys of
3131
a collection individually. Take the following example::
3232

33+
// src/Acme/BlogBundle/Entity/Author.php
3334
namespace Acme\BlogBundle\Entity;
3435

3536
class Author
@@ -53,7 +54,7 @@ blank but is no longer than 100 characters in length, you would do the following
5354

5455
.. code-block:: yaml
5556
56-
# src/BlogBundle/Resources/config/validation.yml
57+
# src/Acme/BlogBundle/Resources/config/validation.yml
5758
Acme\BlogBundle\Entity\Author:
5859
properties:
5960
profileData:

reference/constraints/Email.rst

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,6 @@ Basic Usage
2929
- Email:
3030
message: The email "{{ value }}" is not a valid email.
3131
checkMX: true
32-
.. code-block:: xml
33-
34-
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
35-
<?xml version="1.0" encoding="UTF-8" ?>
36-
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
37-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
38-
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
39-
40-
<class name="Acme\BlogBundle\Entity\Author">
41-
<property name="email">
42-
<constraint name="Email">
43-
<option name="message">The email "{{ value }}" is not a valid email.</option>
44-
<option name="checkMX">true</option>
45-
</constraint>
46-
</property>
47-
</class>
48-
</constraint-mapping>
4932
5033
.. code-block:: php-annotations
5134
@@ -65,6 +48,24 @@ Basic Usage
6548
protected $email;
6649
}
6750
51+
.. code-block:: xml
52+
53+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
54+
<?xml version="1.0" encoding="UTF-8" ?>
55+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
56+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
57+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
58+
59+
<class name="Acme\BlogBundle\Entity\Author">
60+
<property name="email">
61+
<constraint name="Email">
62+
<option name="message">The email "{{ value }}" is not a valid email.</option>
63+
<option name="checkMX">true</option>
64+
</constraint>
65+
</property>
66+
</class>
67+
</constraint-mapping>
68+
6869
.. code-block:: php
6970
7071
// src/Acme/BlogBundle/Entity/Author.php

reference/constraints/False.rst

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ 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-
6251
.. code-block:: php-annotations
6352
6453
// src/Acme/BlogBundle/Entity/Author.php
@@ -69,14 +58,27 @@ method returns **false**:
6958
class Author
7059
{
7160
/**
72-
* @Assert\False()
61+
* @Assert\False(
62+
* message = "You've entered an invalid state."
63+
* )
7364
*/
74-
public function isStateInvalid($message = "You've entered an invalid state.")
65+
public function isStateInvalid()
7566
{
7667
// ...
7768
}
7869
}
7970
71+
.. code-block:: xml
72+
73+
<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
74+
<class name="Acme\BlogBundle\Entity\Author">
75+
<getter property="stateInvalid">
76+
<constraint name="False">
77+
<option name="message">You've entered an invalid state.</option>
78+
</constraint>
79+
</getter>
80+
</class>
81+
8082
.. code-block:: php
8183
8284
// src/Acme/BlogBundle/Entity/Author.php

reference/constraints/File.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,14 @@ below a certain file size and a valid PDF, add the following:
120120
// src/Acme/BlogBundle/Entity/Author.php
121121
namespace Acme\BlogBundle\Entity;
122122
123-
// ...
124123
use Symfony\Component\Validator\Mapping\ClassMetadata;
125-
use Symfony\Component\Validator\Constraints\File;
124+
use Symfony\Component\Validator\Constraints as Assert;
126125
127126
class Author
128127
{
129128
public static function loadValidatorMetadata(ClassMetadata $metadata)
130129
{
131-
$metadata->addPropertyConstraint('bioFile', new File(array(
130+
$metadata->addPropertyConstraint('bioFile', new Assert\File(array(
132131
'maxSize' => '1024k',
133132
'mimeTypes' => array(
134133
'application/pdf',

reference/constraints/UniqueEntity.rst

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ table:
2727

2828
.. configuration-block::
2929

30+
.. code-block:: yaml
31+
32+
# src/Acme/UserBundle/Resources/config/validation.yml
33+
Acme\UserBundle\Entity\Author:
34+
constraints:
35+
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
36+
properties:
37+
email:
38+
- Email: ~
39+
3040
.. code-block:: php-annotations
3141
3242
// Acme/UserBundle/Entity/User.php
@@ -55,28 +65,42 @@ table:
5565
// ...
5666
}
5767
58-
.. code-block:: yaml
59-
60-
# src/Acme/UserBundle/Resources/config/validation.yml
61-
Acme\UserBundle\Entity\Author:
62-
constraints:
63-
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: email
64-
properties:
65-
email:
66-
- Email: ~
67-
6868
.. code-block:: xml
6969
7070
<class name="Acme\UserBundle\Entity\Author">
7171
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
7272
<option name="fields">email</option>
7373
<option name="message">This email already exists.</option>
7474
</constraint>
75-
<property name="email">
75+
<property name="email">
7676
<constraint name="Email" />
7777
</property>
7878
</class>
7979
80+
.. code-block:: php
81+
82+
83+
// Acme/UserBundle/Entity/User.php
84+
namespace Acme\UserBundle\Entity;
85+
86+
use Symfony\Component\Validator\Constraints as Assert;
87+
88+
// DON'T forget this use statement!!!
89+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
90+
91+
class Author
92+
{
93+
public static function loadValidatorMetadata(ClassMetadata $metadata)
94+
{
95+
$metadata->addConstraint(new UniqueEntity(array(
96+
'fields' => 'email',
97+
'message' => 'This email already exists.',
98+
)));
99+
100+
$metadata->addPropertyConstraint(new Assert\Email());
101+
}
102+
}
103+
80104
Options
81105
-------
82106

0 commit comments

Comments
 (0)