8000 Fix the documentation of the Traverse constraint · Issue #12967 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Fix the documentation of the Traverse constraint #12967

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

Closed
guilliamxavier opened this issue Jan 21, 2020 · 1 comment · Fixed by #13491
Closed

Fix the documentation of the Traverse constraint #12967

guilliamxavier opened this issue Jan 21, 2020 · 1 comment · Fixed by #13491

Comments

@guilliamxavier
Copy link
Contributor
guilliamxavier commented Jan 21, 2020

#11310 (comment)

The current code from https://symfony.com/doc/4.4/reference/constraints/Traverse.html:

<?php
// src/Entity/Book.php
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @Assert\Traverse
 */
class Book
{
    /**
     * @var Author
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\Author")
     */
    protected $author;

    /**
     * @var Editor
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\Editor")
     */
    protected $editor;

    // ...
}

throws a \Symfony\Component\Validator\Exception\ConstraintDefinitionException with message Traversal was enabled for "App\Entity\Book", but this class does not implement "\Traversable". (from vendor/symfony/validator/Validator/RecursiveContextualValidator.php).

From symfony/symfony#10287:

Control traversal at class level (symfony/symfony#8617)

Currently, it is possible whether to traverse a Traversable object or not in the Valid constraint:

/**
 * @Assert\Valid(traverse=true)
 */
private $tags = new TagList();

(actually, true is the default)

In this way, the validator will iterate the TagList instance and validate each of the contained objects. You can also set "traverse" to false to disable iteration.

What if you want to specify, that TagList instances should always (or never) be traversed? That's currently not possible.

With this PR, you can do the following:

/**
 * @Assert\Traverse(false)
 */
class TagList implements \IteratorAggregate
{
    // ...
}

I understand that the Traverse constraint is not at all a "shorthand" for Valid constraints on all nested objects, but rather a means to always disable validator traversal on a custom \Traversable class. (But I may be wrong)

@HeahDude
Copy link
Contributor
HeahDude commented Apr 5, 2020

Thanks @guilliamxavier for opening this issue. Sorry for the delay, see #13491.

@HeahDude HeahDude added this to the 3.4 milestone Apr 5, 2020
HeahDude added a commit that referenced this issue Apr 12, 2020
This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] Fixed `Traverse` constraint reference

I'm sorry I've messed up things in #11310. Now I see that I missed #12967, this fixes it.

Commits
-------

ef5c989 [Validator] Fixed `Traverse` constraint reference
@xabbuh xabbuh closed this as completed Apr 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0