|
| 1 | +Cascade |
| 2 | +======= |
| 3 | + |
| 4 | +The Cascade constraint is used to validate a whole class, including all the |
| 5 | +objects that might be stored in its properties. Thanks to this constraint, |
| 6 | +you don't need to add the :doc:`/reference/constraints/Valid` constraint on |
| 7 | +every child object that you want to validate in your class. |
| 8 | + |
| 9 | +========== =================================================================== |
| 10 | +Applies to :ref:`class <validation-class-target>` |
| 11 | +Class :class:`Symfony\\Component\\Validator\\Constraints\\Cascade` |
| 12 | +========== =================================================================== |
| 13 | + |
| 14 | +Basic Usage |
| 15 | +----------- |
| 16 | + |
| 17 | +In the following example, the |
| 18 | +:class:`Symfony\\Component\\Validator\\Constraints\\Cascade` constraint |
| 19 | +will tell the validator to validate all properties of the class, including |
| 20 | +constraints that are set in the child classes ``BookMetadata`` and |
| 21 | +``Author``: |
| 22 | + |
| 23 | +.. configuration-block:: |
| 24 | + |
| 25 | + .. code-block:: php-attributes |
| 26 | +
|
| 27 | + // src/Model/BookCollection.php |
| 28 | + namespace App\Model; |
| 29 | +
|
| 30 | + use App\Model\Author; |
| 31 | + use App\Model\BookMetadata; |
| 32 | + use Symfony\Component\Validator\Constraints as Assert; |
| 33 | +
|
| 34 | + #[Assert\Cascade] |
| 35 | + class BookCollection |
| 36 | + { |
| 37 | + #[Assert\NotBlank] |
| 38 | + protected $name = ''; |
| 39 | +
|
| 40 | + public BookMetadata $metadata; |
| 41 | +
|
| 42 | + public Author $author; |
| 43 | +
|
| 44 | + // ... |
| 45 | + } |
| 46 | +
|
| 47 | + .. code-block:: yaml |
| 48 | +
|
| 49 | + # config/validator/validation.yaml |
| 50 | + App\Entity\BookCollection: |
| 51 | + constraints: |
| 52 | + - Cascade: ~ |
| 53 | +
|
| 54 | + .. code-block:: xml |
| 55 | +
|
| 56 | + <!-- config/validator/validation.xml --> |
| 57 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 58 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 59 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 60 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 61 | +
|
| 62 | + <class name="App\Entity\BookCollection"> |
| 63 | + <constraint name="Cascade"/> |
| 64 | + </class> |
| 65 | + </constraint-mapping> |
| 66 | +
|
| 67 | + .. code-block:: php |
| 68 | +
|
| 69 | + // src/Entity/BookCollection.php |
| 70 | + namespace App\Entity; |
| 71 | +
|
| 72 | + use Symfony\Component\Validator\Constraints as Assert; |
| 73 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 74 | +
|
| 75 | + class BookCollection |
| 76 | + { |
| 77 | + // ... |
| 78 | +
|
| 79 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 80 | + { |
| 81 | + $metadata->addConstraint(new Assert\Cascade()); |
| 82 | + } |
| 83 | + } |
| 84 | +
|
| 85 | +Options |
| 86 | +------- |
| 87 | + |
| 88 | +The ``groups`` option is not available for this constraint. |
| 89 | + |
| 90 | +.. include:: /reference/constraints/_payload-option.rst.inc |
0 commit comments