Valid

Valid

This constraint is used to enable validation on objects that are embedded as properties on an object being validated. This allows you to validate an object and all sub-objects associated with it.

Applies toproperty or method
Options
ClassSymfony\Component\Validator\Constraints\Valid

Tip

By default, the error_bubbling option is enabled for the collection Field Type, which passes the errors to the parent form. If you want to attach the errors to the locations where they actually occur you have to set error_bubbling to false.

Basic Usage

In the following example, create two classes Author and Address that both have constraints on their properties. Furthermore, Author stores an Address instance in the $address property:

  1. // src/Entity/Address.php
  2. namespace App\Entity;
  3. class Address
  4. {
  5. protected $street;
  6. protected $zipCode;
  7. }
  1. // src/Entity/Author.php
  2. namespace App\Entity;
  3. class Author
  4. {
  5. protected $firstName;
  6. protected $lastName;
  7. protected $address;
  8. }
  • Annotations

    1. // src/Entity/Address.php
    2. namespace App\Entity;
    3. use Symfony\Component\Validator\Constraints as Assert;
    4. class Address
    5. {
    6. /**
    7. * @Assert\NotBlank
    8. */
    9. protected $street;
    10. /**
    11. * @Assert\NotBlank
    12. * @Assert\Length(max=5)
    13. */
    14. protected $zipCode;
    15. }
    16. // src/Entity/Author.php
    17. namespace App\Entity;
    18. use Symfony\Component\Validator\Constraints as Assert;
    19. class Author
    20. {
    21. /**
    22. * @Assert\NotBlank
    23. * @Assert\Length(min=4)
    24. */
    25. protected $firstName;
    26. /**
    27. * @Assert\NotBlank
    28. */
    29. protected $lastName;
    30. protected $address;
    31. }
  • YAML

    1. # config/validator/validation.yaml
    2. App\Entity\Address:
    3. properties:
    4. street:
    5. - NotBlank: ~
    6. zipCode:
    7. - NotBlank: ~
    8. - Length:
    9. max: 5
    10. App\Entity\Author:
    11. properties:
    12. firstName:
    13. - NotBlank: ~
    14. - Length:
    15. min: 4
    16. lastName:
    17. - NotBlank: ~
  • 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\Address">
    7. <property name="street">
    8. <constraint name="NotBlank"/>
    9. </property>
    10. <property name="zipCode">
    11. <constraint name="NotBlank"/>
    12. <constraint name="Length">
    13. <option name="max">5</option>
    14. </constraint>
    15. </property>
    16. </class>
    17. <class name="App\Entity\Author">
    18. <property name="firstName">
    19. <constraint name="NotBlank"/>
    20. <constraint name="Length">
    21. <option name="min">4</option>
    22. </constraint>
    23. </property>
    24. <property name="lastName">
    25. <constraint name="NotBlank"/>
    26. </property>
    27. </class>
    28. </constraint-mapping>
  • PHP

    1. // src/Entity/Address.php
    2. namespace App\Entity;
    3. use Symfony\Component\Validator\Constraints as Assert;
    4. use Symfony\Component\Validator\Mapping\ClassMetadata;
    5. class Address
    6. {
    7. public static function loadValidatorMetadata(ClassMetadata $metadata)
    8. {
    9. $metadata->addPropertyConstraint('street', new Assert\NotBlank());
    10. $metadata->addPropertyConstraint('zipCode', new Assert\NotBlank());
    11. $metadata->addPropertyConstraint('zipCode', new Assert\Length(['max' => 5]));
    12. }
    13. }
    14. // src/Entity/Author.php
    15. namespace App\Entity;
    16. use Symfony\Component\Validator\Constraints as Assert;
    17. use Symfony\Component\Validator\Mapping\ClassMetadata;
    18. class Author
    19. {
    20. public static function loadValidatorMetadata(ClassMetadata $metadata)
    21. {
    22. $metadata->addPropertyConstraint('firstName', new Assert\NotBlank());
    23. $metadata->addPropertyConstraint('firstName', new Assert\Length(['min' => 4]));
    24. $metadata->addPropertyConstraint('lastName', new Assert\NotBlank());
    25. }
    26. }

With this mapping, it is possible to successfully validate an author with an invalid address. To prevent that, add the Valid constraint to the $address property.

  • Annotations

    1. // src/Entity/Author.php
    2. namespace App\Entity;
    3. use Symfony\Component\Validator\Constraints as Assert;
    4. class Author
    5. {
    6. /**
    7. * @Assert\Valid
    8. */
    9. protected $address;
    10. }
  • YAML

    1. # config/validator/validation.yaml
    2. App\Entity\Author:
    3. properties:
    4. address:
    5. - Valid: ~
  • 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\Author">
    7. <property name="address">
    8. <constraint name="Valid"/>
    9. </property>
    10. </class>
    11. </constraint-mapping>
  • PHP

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

If you validate an author with an invalid address now, you can see that the validation of the Address fields failed.

  1. App\Entity\Author.address.zipCode:
  2. This value is too long. It should have 5 characters or less.

Tip

If you also want to validate that the address property is an instance of the App\Entity\Address class, add the Type constraint.

Options

groups

type: array | string

It defines the validation group or groups this constraint belongs to. Read more about validation groups.

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.

traverse

type: boolean default: true

If this constraint is applied to a \Traversable, then all containing values will be validated if this option is set to true. This option is ignored on arrays: Arrays are traversed in either case. Keys are not validated.

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