Sébastien
Sébastien

Reputation: 1697

Cascade validation on entities with Symfony2

I'm wondering how I can perform a cascade validation on an entity with the validator service.

Using:

$validator = $this->get('validator');
$errorList = $validator->validate($entity);

does not perform a validation of the "entity" children entities.

I'm pretty sure it can be done since its possible using the form validation (using 'cascade_validation' => true).

Upvotes: 3

Views: 3163

Answers (1)

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

I think that (by default) validation is not processed on attributes that points to objects. It can be done for arrays with the traverse option of the validate() method set to true.

To bypass this limitation, you should consider using a Valid Constraint.

Check this well explained example of using the Valid Constraint and where processing validation on the parent object also checks the child's object validation rules.

The Valid Constraint also provides a traverse option that you can use to check attributes that point to an array of objects.

Upvotes: 6

Related Questions