Traverse

Traverse

Object properties are only validated if they are accessible, either by being public or having public accessor methods (e.g. a public getter). If your object needs to be traversed to validate its data, you can use this constraint.

Applies toclass
Options
ClassSymfony\Component\Validator\Constraints\Traverse

Basic Usage

In the following example, create two classes BookCollection and Book that all have constraints on their properties.

  • Annotations

    1. // src/Entity/BookCollection.php
    2. namespace App\Entity;
    3. use Doctrine\Collections\ArrayCollection;
    4. use Doctrine\Collections\Collection;
    5. use Doctrine\ORM\Mapping as ORM;
    6. use Symfony\Component\Validator\Constraints as Assert;
    7. /**
    8. * @ORM\Entity
    9. * @Assert\Traverse
    10. */
    11. class BookCollection implements \IteratorAggregate
    12. {
    13. /**
    14. * @var string
    15. *
    16. * @ORM\Column
    17. *
    18. * @Assert\NotBlank
    19. */
    20. protected $name = '';
    21. /**
    22. * @var Collection|Book[]
    23. *
    24. * @ORM\ManyToMany(targetEntity="App\Entity\Book")
    25. */
    26. protected $books;
    27. // some other properties
    28. public function __construct()
    29. {
    30. $this->books = new ArrayCollection();
    31. }
    32. // ... setter for name, adder and remover for books
    33. // the name can be validated by calling the getter
    34. public function getName(): string
    35. {
    36. return $this->name;
    37. }
    38. /**
    39. * @return \Generator|Book[] The books for a given author
    40. */
    41. public function getBooksForAuthor(Author $author): iterable
    42. {
    43. foreach ($this->books as $book) {
    44. if ($book->isAuthoredBy($author)) {
    45. yield $book;
    46. }
    47. }
    48. }
    49. // neither the method above nor any other specific getter
    50. // could be used to validated all nested books;
    51. // this object needs to be traversed to call the iterator
    52. public function getIterator()
    53. {
    54. return $this->books->getIterator();
    55. }
    56. }
  • YAML

    1. # config/validator/validation.yaml
    2. App\Entity\BookCollection:
    3. constraints:
    4. - Traverse: ~
  • XML

    1. <!-- config/validator/validation.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
    6. <class name="App\Entity\BookCollection">
    7. <constraint name="Traverse"/>
    8. </class>
    9. </constraint-mapping>
  • PHP

    1. // src/Entity/BookCollection.php
    2. namespace App\Entity;
    3. use Symfony\Component\Validator\Constraints as Assert;
    4. use Symfony\Component\Validator\Mapping\ClassMetadata;
    5. class BookCollection
    6. {
    7. // ...
    8. public static function loadValidatorMetadata(ClassMetadata $metadata)
    9. {
    10. $metadata->addConstraint(new Assert\Traverse());
    11. }
    12. }

When the object implements \Traversable (like here with its child \IteratorAggregate), its traversal strategy will implicitly be set and the object will be iterated over without defining the constraint. It’s mostly useful to add it to be explicit or to disable the traversal using the traverse option. If a public getter exists to return the inner books collection like getBooks(): Collection, the Valid constraint can be used on the $books property instead.

Options

The groups option is not available for this constraint.

traverse

type: boolean default: true

Instances of \Traversable are traversed by default, use this option to disable validating:

  • Annotations

    1. // src/Entity/BookCollection.php
    2. // ... same as above
    3. /**
    4. * ...
    5. * @Assert\Traverse(false)
    6. */
    7. class BookCollection implements \IteratorAggregate
    8. {
    9. // ...
    10. }
  • YAML

    1. # config/validator/validation.yaml
    2. App\Entity\BookCollection:
    3. constraints:
    4. - Traverse: false
  • XML

    1. <!-- config/validator/validation.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
    6. <class name="App\Entity\BookCollection">
    7. <constraint name="Traverse">false</constraint>
    8. </class>
    9. </constraint-mapping>
  • PHP

    1. // src/Entity/BookCollection.php
    2. namespace App\Entity;
    3. use Symfony\Component\Validator\Constraints as Assert;
    4. use Symfony\Component\Validator\Mapping\ClassMetadata;
    5. class BookCollection
    6. {
    7. // ...
    8. public static function loadValidatorMetadata(ClassMetadata $metadata)
    9. {
    10. $metadata->addConstraint(new Assert\Traverse(false));
    11. }
    12. }

payload

type: mixed default: null

This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to you.

For example, you may want to use several error levels to present failed constraints differently in the front-end depending on the severity of the error.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.