diff --git a/reference/constraints/Cascade.rst b/reference/constraints/Cascade.rst new file mode 100644 index 00000000000..c4127e6c9dc --- /dev/null +++ b/reference/constraints/Cascade.rst @@ -0,0 +1,120 @@ +Cascade +======= + +.. versionadded:: 5.2 + + The :class:`Symfony\\Component\\Validator\\Constraints\\Cascade` was + introduced in Symfony 5.2 and requires PHP 7.4. + +The Cascade constraint is used to validate a whole class. It allows to +omit to add the :doc:`/reference/constraints/Valid` constraint on each +child object of your class you want to validate. + +========== =================================================================== +Applies to :ref:`class ` +Class :class:`Symfony\\Component\\Validator\\Constraints\\Cascade` +========== =================================================================== + +Basic Usage +----------- + +In the following example, the +:class:`Symfony\\Component\\Validator\\Constraints\\Cascade` constraint +will tell the validator to validate all fields of the class, including +constraints that are set in the child classes ``BookMetadata`` and +``Author``: + +.. configuration-block:: + + .. code-block:: php-annotations + + // src/Model/BookCollection.php + namespace App\Model; + + use App\Model\Author; + use App\Model\BookMetadata; + use Symfony\Component\Validator\Constraints as Assert; + + /** + * @Assert\Cascade + */ + class BookCollection + { + /** + * @Assert\NotBlank + */ + protected $name = ''; + + public BookMetadata $metadata; + + public Author $author; + + // ... + } + + .. code-block:: php-attributes + + // src/Model/BookCollection.php + namespace App\Model; + + use App\Model\Author; + use App\Model\BookMetadata; + use Symfony\Component\Validator\Constraints as Assert; + + #[Assert\Cascade] + class BookCollection + { + #[Assert\NotBlank] + protected $name = ''; + + public BookMetadata $metadata; + + public Author $author; + + // ... + } + + .. code-block:: yaml + + # config/validator/validation.yaml + App\Entity\BookCollection: + constraints: + - Cascade: ~ + + .. code-block:: xml + + + + + + + + + + + .. code-block:: php + + // src/Entity/BookCollection.php + namespace App\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + use Symfony\Component\Validator\Mapping\ClassMetadata; + + class BookCollection + { + // ... + + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addConstraint(new Assert\Cascade()); + } + } + +Options +------- + +The ``groups`` option is not available for this constraint. + +.. include:: /reference/constraints/_payload-option.rst.inc diff --git a/reference/constraints/map.rst.inc b/reference/constraints/map.rst.inc index 9f8eb4b8c3f..a0c1324dd1e 100644 --- a/reference/constraints/map.rst.inc +++ b/reference/constraints/map.rst.inc @@ -96,6 +96,7 @@ Other Constraints * :doc:`Expression ` * :doc:`All ` * :doc:`Valid ` +* :doc:`Cascade ` * :doc:`Traverse ` * :doc:`Collection ` * :doc:`Count `