Skip to content

[Validator] Added documentation for Traverse constraint #11310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions reference/constraints/Traverse.rst
Original file line number Diff line number Diff line change
@@ -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 <validation-class-target>` |
+----------------+-------------------------------------------------------------------------------------+
| Options | - `payload`_ |
+----------------+-------------------------------------------------------------------------------------+
| Class | :class:`Symfony\\Bridge\\Doctrine\\Validator\\Constraints\\Traverse` |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why isn't there any dedicated validator for this specific constraint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #11320. Like Valid those constraints are handled by the recursive context itself to define the cascade strategy, so do not need custom validator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, TIL, thanks 😄

+----------------+-------------------------------------------------------------------------------------+

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

<!-- src/AppBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">

<class name="AppBundle\Entity\Book">
<constraint name="Symfony\Component\Validator\Constraints\Traverse"/>
</class>
</constraint-mapping>

.. 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