Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
Description
The current code fromhttps://symfony.com/doc/4.4/reference/constraints/Traverse.html:
<?php// src/Entity/Book.phpnamespaceApp\Entity;useDoctrine\ORM\MappingasORM;useSymfony\Component\Validator\ConstraintsasAssert;/** * @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 messageTraversal was enabled for "App\Entity\Book", but this class does not implement "\Traversable".
(fromvendor/symfony/validator/Validator/RecursiveContextualValidator.php
).
Control traversal at class level (symfony/symfony#8617)
Currently, it is possible whether to traverse a
Traversable
object or not in theValid
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" tofalse
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 theTraverse
constraint is not at all a "shorthand" forValid
constraints on all nested objects, but rather a means to always disable validator traversal on a custom\Traversable
class. (But I may be wrong)