Home > OS >  Symfony Validation on Relations
Symfony Validation on Relations

Time:01-09

Suppose I have the following Entities: Pizza PizzaType = [Cheese, Meat, Vegetable, Supreme] Toppings = [Pepperoni, Sausage, Peppers, Mushrooms]

When I create a pizza and set its type to Meat I want to be able to validate that only a "meat" type of topping is chosen, same with Vegetable, while supreme will allow any topping (meat or vegetable)... Assume there is already a property for each topping that maps to a pizza type. Really just interested in know what type of validator to use...

CodePudding user response:

You can pass a callback to validation_groups option in your form for the desired field. Callback will have access to submitted data like this

... 'validation_groups' => static function (FormInterface $form) {

$data = $form->getData();

// your logic to determine validation groups

}

in there you can check what has been submitted and return an array of validation groups, different for each type of entity you want.

You can find more info here https://symfony.com/doc/current/form/data_based_validation.html

  •  Tags:  
  • Related