From 86ee59ed92df3d3ef66b6b2fef890215b3d929e9 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Sat, 6 Apr 2019 14:25:29 +0200 Subject: [PATCH] [Validator] Added documentation for Traverse constraint --- reference/constraints/Traverse.rst | 98 ++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 reference/constraints/Traverse.rst diff --git a/reference/constraints/Traverse.rst b/reference/constraints/Traverse.rst new file mode 100644 index 00000000000..28f3580eb6b --- /dev/null +++ b/reference/constraints/Traverse.rst @@ -0,0 +1,98 @@ +Traverse +======== + +Objects do not validate nested objects by default unless explicitly using +this constraint. +If only specific nested objects should be validated by cascade, consider +using the :doc:`references/constraints/Valid` instead. + ++----------------+-------------------------------------------------------------------------------------+ +| Applies to | :ref:`class ` | ++----------------+-------------------------------------------------------------------------------------+ +| Options | - `payload`_ | ++----------------+-------------------------------------------------------------------------------------+ +| Class | :class:`Symfony\\Bridge\\Doctrine\\Validator\\Constraints\\Traverse` | ++----------------+-------------------------------------------------------------------------------------+ + +Basic Usage +----------- + +In the following example, create three classes ``Book``, ``Author`` and +``Editor`` that all have constraints on their properties. Furthermore, +``Book`` stores an ``Author`` and an ``Editor`` instance that must be +valid too. Instead of adding the ``Valid`` constraint to both fields, +configure the ``Traverse`` constraint on the ``Book`` class. + +.. configuration-block:: + + .. code-block:: php-annotations + + // src/AppBundle/Entity/Book.php + namespace AppBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + use Doctrine\ORM\Mapping as ORM; + + /** + * @ORM\Entity + * @Assert\Traverse + */ + class Book + { + /** + * @var Author + * + * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Author") + */ + protected $author; + + /** + * @var Editor + * + * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Editor") + */ + protected $editor; + + // ... + } + + .. code-block:: yaml + + # src/AppBundle/Resources/config/validation.yml + AppBundle\Entity\Book: + constraints: + - Symfony\Component\Validator\Constraints\Traverse: ~ + + .. code-block:: xml + + + + + + + + + + + .. code-block:: php + + // src/AppBundle/Entity/Book.php + namespace AppBundle\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + use Symfony\Component\Validator\Mapping\ClassMetadata; + + class Book + { + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addConstraint(new Assert\Traverse()); + } + } + +Options +------- + +.. include:: /reference/constraints/_payload-option.rst.inc