Skip to content

Commit 5dd6f2c

Browse files
[Validator] Add Cascade constraint documentation
1 parent 1c33f40 commit 5dd6f2c

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

reference/constraints/Cascade.rst

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
Cascade
2+
=======
3+
4+
.. versionadded:: 5.2
5+
6+
The :class:`Symfony\\Component\\Validator\\Constraints\\Cascade` was
7+
introduced in Symfony 5.2 and requires PHP 7.4.
8+
9+
The Cascade constraint is used to validate a whole class. It allows to
10+
omit to add the :doc:`/reference/constraints/Valid` constraint on each
11+
child object of your class you want to validate.
12+
13+
========== ===================================================================
14+
Applies to :ref:`class <validation-class-target>`
15+
Class :class:`Symfony\\Component\\Validator\\Constraints\\Cascade`
16+
========== ===================================================================
17+
18+
Basic Usage
19+
-----------
20+
21+
In the following example, the
22+
:class:`Symfony\\Component\\Validator\\Constraints\\Cascade` constraint
23+
will tell the validator to validate all fields of the class, including
24+
constraints that are set in the child classes ``BookMetadata`` and
25+
``Author``:
26+
27+
.. configuration-block::
28+
29+
.. code-block:: php-annotations
30+
31+
// src/Model/BookCollection.php
32+
namespace App\Model;
33+
34+
use App\Model\Author;
35+
use App\Model\BookMetadata;
36+
use Symfony\Component\Validator\Constraints as Assert;
37+
38+
/**
39+
* @Assert\Cascade
40+
*/
41+
class BookCollection
42+
{
43+
/**
44+
* @Assert\NotBlank
45+
*/
46+
protected $name = '';
47+
48+
public BookMetadata $metadata;
49+
50+
public Author $author;
51+
52+
// ...
53+
}
54+
55+
.. code-block:: php-attributes
56+
57+
// src/Model/BookCollection.php
58+
namespace App\Model;
59+
60+
use App\Model\Author;
61+
use App\Model\BookMetadata;
62+
use Symfony\Component\Validator\Constraints as Assert;
63+
64+
#[Assert\Cascade]
65+
class BookCollection
66+
{
67+
#[Assert\NotBlank]
68+
protected $name = '';
69+
70+
public BookMetadata $metadata;
71+
72+
public Author $author;
73+
74+
// ...
75+
}
76+
77+
.. code-block:: yaml
78+
79+
# config/validator/validation.yaml
80+
App\Entity\BookCollection:
81+
constraints:
82+
- Cascade: ~
83+
84+
.. code-block:: xml
85+
86+
<!-- config/validator/validation.xml -->
87+
<?xml version="1.0" encoding="UTF-8" ?>
88+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
89+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
90+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
91+
92+
<class name="App\Entity\BookCollection">
93+
<constraint name="Cascade"/>
94+
</class>
95+
</constraint-mapping>
96+
97+
.. code-block:: php
98+
99+
// src/Entity/BookCollection.php
100+
namespace App\Entity;
101+
102+
use Symfony\Component\Validator\Constraints as Assert;
103+
use Symfony\Component\Validator\Mapping\ClassMetadata;
104+
105+
class BookCollection
106+
{
107+
// ...
108+
109+
public static function loadValidatorMetadata(ClassMetadata $metadata)
110+
{
111+
$metadata->addConstraint(new Assert\Cascade());
112+
}
113+
}
114+
115+
Options
116+
-------
117+
118+
The ``groups`` option is not available for this constraint.
119+
120+
.. include:: /reference/constraints/_payload-option.rst.inc

reference/constraints/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Other Constraints
9696
* :doc:`Expression </reference/constraints/Expression>`
9797
* :doc:`All </reference/constraints/All>`
9898
* :doc:`Valid </reference/constraints/Valid>`
99+
* :doc:`Cascade </reference/constraints/Cascade>`
99100
* :doc:`Traverse </reference/constraints/Traverse>`
100101
* :doc:`Collection </reference/constraints/Collection>`
101102
* :doc:`Count </reference/constraints/Count>`

0 commit comments

Comments
 (0)