Symfony2: recursive validation

I have an entity with some validators ( not a form ).

Therefore, I use $validator->validate($entity) , but it does not check my sub-objects (the entity class has some other entity classes with some validators).

Is there an β€œautomatic” way to do this, or do I need to do $errorList->addAll($validator->validate($entity)); for each of them?

+4
source share
1 answer

To enable recursive checking on objects, you can simply use Constraint @Assert\Valid

Example
Say the person has a mandatory last name

 class Person { /** * @Assert\NotNull * @var string */ protected $lastName; } 

And you have a product that has a buyer ( Person )

 class Product { /** * @Assert\NotNull * @Assert\Valid * @var Person */ protected $buyer; } 

Having NotNull and Valid , every time you check the Product model, it checks that:

  • This one has a buyer
  • Buyer has lastName
+6
source

Source: https://habr.com/ru/post/1498600/


All Articles