I've got an entity with some validators (not a form).
So I use $validator->validate($entity)
, but it doesn't validate my sub-ojects (the entity class has some others entity classes with some validators).
Is there an "automatic" way to do this, or I have to do $errorList->addAll($validator->validate($entity));
for each of them ?
To allow recursive validation over objects you can simply use the Constraint @Assert\Valid
Example
Say a person has a mandatory last name
class Person
{
/**
* @Assert\NotNull
* @var string
*/
protected $lastName;
}
And you have a product, which have a buyer (Person
)
class Product
{
/**
* @Assert\NotNull
* @Assert\Valid
* @var Person
*/
protected $buyer;
}
By having NotNull
and Valid
, each time you validate the Product
model it will check that:
lastName
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With