Skip to content

Commit 0397541

Browse files
Bernhard Schussekfabpot
Bernhard Schussek
authored andcommitted
Adapted documentation of Valid constraint to latest changes
1 parent ff15da6 commit 0397541

File tree

1 file changed

+17
-61
lines changed

1 file changed

+17
-61
lines changed

guides/validator/constraints/Valid.rst

+17-61
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
Valid
22
=====
33

4-
Validates an associated object.
4+
Marks an associated object to be validated itself.
55

66
.. code-block:: yaml
77
88
properties:
99
address:
1010
- Valid: ~
1111
12-
Options
13-
-------
14-
15-
* ``class``: The expected class of the object
16-
* ``message``: The error message if the class doesn't match
17-
1812
Example: Validate object graphs
1913
-------------------------------
2014

@@ -89,15 +83,15 @@ their properties. Furthermore, ``Author`` stores an ``Address`` instance in the
8983
.. code-block:: php-annotations
9084
9185
// Application/HelloBundle/Address.php
92-
class Author
86+
class Address
9387
{
9488
/**
9589
* @validation:NotBlank()
9690
*/
9791
protected $street;
9892
9993
/**
100-
* @validation:NotBlank()
94+
* @validation:NotBlank
10195
* @validation:MaxLength(5)
10296
*/
10397
protected $zipCode;
@@ -107,15 +101,17 @@ their properties. Furthermore, ``Author`` stores an ``Address`` instance in the
107101
class Author
108102
{
109103
/**
110-
* @validation:NotBlank()
104+
* @validation:NotBlank
111105
* @validation:MinLength(4)
112106
*/
113107
protected $firstName;
114108
115109
/**
116-
* @validation:NotBlank()
110+
* @validation:NotBlank
117111
*/
118112
protected $lastName;
113+
114+
protected $address;
119115
}
120116
121117
.. code-block:: php
@@ -124,7 +120,7 @@ their properties. Furthermore, ``Author`` stores an ``Address`` instance in the
124120
use Symfony\Components\Validator\Constraints\NotBlank;
125121
use Symfony\Components\Validator\Constraints\MaxLength;
126122
127-
class Author
123+
class Address
128124
{
129125
protected $street;
130126
@@ -148,6 +144,8 @@ their properties. Furthermore, ``Author`` stores an ``Address`` instance in the
148144
149145
protected $lastName;
150146
147+
protected $address;
148+
151149
public static function loadMetadata(ClassMetadata $metadata)
152150
{
153151
$metadata->addPropertyConstraint('firstName', new NotBlank());
@@ -184,8 +182,10 @@ invalid address. To prevent that, we add the ``Valid`` constraint to the
184182
// Application/HelloBundle/Author.php
185183
class Author
186184
{
185+
/* ... */
186+
187187
/**
188-
* @validation:Valid()
188+
* @validation:Valid
189189
*/
190190
protected $address;
191191
}
@@ -205,52 +205,8 @@ invalid address. To prevent that, we add the ``Valid`` constraint to the
205205
}
206206
}
207207
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.
242210

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\HelloBundle\Author.address.zipCode:
212+
This value is too long. It should have 5 characters or less

0 commit comments

Comments
 (0)