|
| 1 | +Traverse |
| 2 | +======== |
| 3 | + |
| 4 | +Objects do not validate nested objects by default unless explicitly using |
| 5 | +this constraint. |
| 6 | +If only specific nested objects should be validated by cascade, consider |
| 7 | +using the :doc:`references/constraints/Valid` instead. |
| 8 | + |
| 9 | ++----------------+-------------------------------------------------------------------------------------+ |
| 10 | +| Applies to | :ref:`class <validation-class-target>` | |
| 11 | ++----------------+-------------------------------------------------------------------------------------+ |
| 12 | +| Options | - `payload`_ | |
| 13 | ++----------------+-------------------------------------------------------------------------------------+ |
| 14 | +| Class | :class:`Symfony\\Bridge\\Doctrine\\Validator\\Constraints\\Traverse` | |
| 15 | ++----------------+-------------------------------------------------------------------------------------+ |
| 16 | + |
| 17 | +Basic Usage |
| 18 | +----------- |
| 19 | + |
| 20 | +In the following example, create three classes ``Book``, ``Author`` and |
| 21 | +``Editor`` that all have constraints on their properties. Furthermore, |
| 22 | +``Book`` stores an ``Author`` and an ``Editor`` instance that must be |
| 23 | +valid too. Instead of adding the ``Valid`` constraint to both fields, |
| 24 | +configure the ``Traverse`` constraint on the ``Book`` class. |
| 25 | + |
| 26 | +.. configuration-block:: |
| 27 | + |
| 28 | + .. code-block:: php-annotations |
| 29 | +
|
| 30 | + // src/AppBundle/Entity/Book.php |
| 31 | + namespace AppBundle\Entity; |
| 32 | +
|
| 33 | + use Symfony\Component\Validator\Constraints as Assert; |
| 34 | + use Doctrine\ORM\Mapping as ORM; |
| 35 | +
|
| 36 | + /** |
| 37 | + * @ORM\Entity |
| 38 | + * @Assert\Traverse |
| 39 | + */ |
| 40 | + class Book |
| 41 | + { |
| 42 | + /** |
| 43 | + * @var Author |
| 44 | + * |
| 45 | + * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Author") |
| 46 | + */ |
| 47 | + protected $author; |
| 48 | +
|
| 49 | + /** |
| 50 | + * @var Editor |
| 51 | + * |
| 52 | + * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Editor") |
| 53 | + */ |
| 54 | + protected $editor; |
| 55 | +
|
| 56 | + // ... |
| 57 | + } |
| 58 | +
|
| 59 | + .. code-block:: yaml |
| 60 | +
|
| 61 | + # src/AppBundle/Resources/config/validation.yml |
| 62 | + AppBundle\Entity\Book: |
| 63 | + constraints: |
| 64 | + - Symfony\Component\Validator\Constraints\Traverse: ~ |
| 65 | +
|
| 66 | + .. code-block:: xml |
| 67 | +
|
| 68 | + <!-- src/AppBundle/Resources/config/validation.xml --> |
| 69 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 70 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 71 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 72 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 73 | +
|
| 74 | + <class name="AppBundle\Entity\Book"> |
| 75 | + <constraint name="Symfony\Component\Validator\Constraints\Traverse"/> |
| 76 | + </class> |
| 77 | + </constraint-mapping> |
| 78 | +
|
| 79 | + .. code-block:: php |
| 80 | +
|
| 81 | + // src/AppBundle/Entity/Book.php |
| 82 | + namespace AppBundle\Entity; |
| 83 | +
|
| 84 | + use Symfony\Component\Validator\Constraints as Assert; |
| 85 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 86 | +
|
| 87 | + class Book |
| 88 | + { |
| 89 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 90 | + { |
| 91 | + $metadata->addConstraint(new Assert\Traverse()); |
| 92 | + } |
| 93 | + } |
| 94 | +
|
| 95 | +Options |
| 96 | +------- |
| 97 | + |
| 98 | +.. include:: /reference/constraints/_payload-option.rst.inc |
0 commit comments