I have a form with many fields and validation groups, these fields contain some view data transformers too.
I need suppress the validation form partially (Groups based on the Submitted Data):
use AppBundle\Entity\Client;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
// ...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'validation_groups' => function (FormInterface $form) {
$data = $form->getData();
if (Client::TYPE_PERSON == $data->getType()) {
return array('person');
}
return array('company');
},
));
}
When you do that, the form will execute the basic integrity checks (Disabling Validation) and the validation errors that coming from the transformers were still thrown (Creating the Transformer).
Use the POST_SUBMIT
event and preventing the ValidationListener from being called (Suppressing Form Validation):
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$event->stopPropagation();
}, 900); // Always set a higher priority than ValidationListener
// ...
}
It is not a solution for me, since accidentally it disables something more than just the form validation.
The question is: How to disable one transformer validation error "dynamically"?
Example:
I have a form field RepeatedType
belonging to person
validation group and contains a view transformer (RepeatedType), this transformer throws an exception when the values in the array aren't the same (ValueToDuplicatesTransformer).
So, even when validation group is company
, the form shows errors belong to RepeatedType
field coming from transformers.
The question here is: How to disable the ValueToDuplicatesTransformer
errors when validation group is not person
?