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