In Symfony I want to validate a form dynamically. Depending on a value of an entity field I want to enable an validation group, in the case named "payed". I use the following code:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\Account',
'validation_groups' => function (FormInterface $form) {
// Set always default
$valarr = ['Default'];
$business = $form->get('business')->getData();
if($business == true) $valarr[] = "business";
// This needs to be added in pending on entity field value
if(???) $valarr[] = "payed";
return $valarr;
},
));
}
How can I get my entity that is know in the controller where this form is activated into this function? For business it works as I can get the value from the form directly.
Like Matteo says in the comment, you can just use $form->getData()->$fieldname
to obtain the value.
if ($form->getData()->$fieldname === 'my_special_value') {
$valarr[] = "payed";
}