1
1
Valid
2
2
=====
3
3
4
- Validates an associated object.
4
+ Marks an associated object to be validated itself .
5
5
6
6
.. code-block :: yaml
7
7
8
8
properties :
9
9
address :
10
10
- Valid : ~
11
11
12
- Options
13
- -------
14
-
15
- * ``class ``: The expected class of the object
16
- * ``message ``: The error message if the class doesn't match
17
-
18
12
Example: Validate object graphs
19
13
-------------------------------
20
14
@@ -89,15 +83,15 @@ their properties. Furthermore, ``Author`` stores an ``Address`` instance in the
89
83
.. code-block :: php-annotations
90
84
91
85
// Application/HelloBundle/Address.php
92
- class Author
86
+ class Address
93
87
{
94
88
/**
95
89
* @validation:NotBlank()
96
90
*/
97
91
protected $street;
98
92
99
93
/**
100
- * @validation:NotBlank()
94
+ * @validation:NotBlank
101
95
* @validation:MaxLength(5)
102
96
*/
103
97
protected $zipCode;
@@ -107,15 +101,17 @@ their properties. Furthermore, ``Author`` stores an ``Address`` instance in the
107
101
class Author
108
102
{
109
103
/**
110
- * @validation:NotBlank()
104
+ * @validation:NotBlank
111
105
* @validation:MinLength(4)
112
106
*/
113
107
protected $firstName;
114
108
115
109
/**
116
- * @validation:NotBlank()
110
+ * @validation:NotBlank
117
111
*/
118
112
protected $lastName;
113
+
114
+ protected $address;
119
115
}
120
116
121
117
.. code-block :: php
@@ -124,7 +120,7 @@ their properties. Furthermore, ``Author`` stores an ``Address`` instance in the
124
120
use Symfony\Components\Validator\Constraints\NotBlank;
125
121
use Symfony\Components\Validator\Constraints\MaxLength;
126
122
127
- class Author
123
+ class Address
128
124
{
129
125
protected $street;
130
126
@@ -148,6 +144,8 @@ their properties. Furthermore, ``Author`` stores an ``Address`` instance in the
148
144
149
145
protected $lastName;
150
146
147
+ protected $address;
148
+
151
149
public static function loadMetadata(ClassMetadata $metadata)
152
150
{
153
151
$metadata->addPropertyConstraint('firstName', new NotBlank());
@@ -184,8 +182,10 @@ invalid address. To prevent that, we add the ``Valid`` constraint to the
184
182
// Application/HelloBundle/Author.php
185
183
class Author
186
184
{
185
+ /* ... */
186
+
187
187
/**
188
- * @validation:Valid()
188
+ * @validation:Valid
189
189
*/
190
190
protected $address;
191
191
}
@@ -205,52 +205,8 @@ invalid address. To prevent that, we add the ``Valid`` constraint to the
205
205
}
206
206
}
207
207
208
- We can even go one step further and validate the class of the related object
209
- to be ``Address `` or one of its subclasses.
210
-
211
- .. configuration-block ::
212
-
213
- .. code-block :: yaml
214
-
215
- # Application/HelloBundle/Resources/config/validation.yml
216
- Application\HelloBundle\Author :
217
- properties :
218
- address :
219
- - Valid : { class: Application\ḨelloBundle\Address }
220
-
221
- .. code-block :: xml
222
-
223
- <!-- Application/HelloBundle/Resources/config/validation.xml -->
224
- <class name =" Application\HelloBundle\Author" >
225
- <property name =" address" >
226
- <constraint name =" Valid" >Application\HelloBundle\Address</constraint >
227
- </property >
228
- </class >
229
-
230
- .. code-block :: php-annotations
231
-
232
- // Application/HelloBundle/Author.php
233
- class Author
234
- {
235
- /**
236
- * @validation:Valid(class = "Application\HelloBundle\Address")
237
- */
238
- protected $address;
239
- }
240
-
241
- .. code-block :: php
208
+ If you validate an author with an invalid address now, you can see that the
209
+ validation of the ``Address `` fields failed.
242
210
243
- // Application/HelloBundle/Author.php
244
- use Symfony\Components\Validator\Constraints\Valid;
245
-
246
- class Author
247
- {
248
- protected $address;
249
-
250
- public static function loadMetadata(ClassMetadata $metadata)
251
- {
252
- $metadata->addPropertyConstraint('address', new Valid(array(
253
- 'class' => 'Application\HelloBundle\Address',
254
- )));
255
- }
256
- }
211
+ Application\H elloBundle\A uthor.address.zipCode:
212
+ This value is too long. It should have 5 characters or less
0 commit comments