How to disable one transformer check error "dynamically" in Symfony2

I have a form with many fields and validation groups, these fields also contain some data converters.

I need to partially close the verification form ( Groups based on 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 this, the form will still carry out integrity checks ( Disabling verification ) and verification errors coming from the transformers that they throw out ( Creating a transformer ).

Use the POST_SUBMIT event and do not call the ValidationListener ( Suppression of form confirmation ):

 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 // ... } 

This is not a solution for me, as you accidentally disable something more than just form validation.

Question: How to disable one transformer check error "dynamically"?

Example:

I have a RepeatedType form RepeatedType that belongs to the person verification group and contains a transformer of the form ( RepeatedType ), this transformer throws an exception if the values ​​in the array do not match ( ValueToDuplicatesTransformer ).

Thus, even if the verification group is company , the form shows errors related to the RepeatedType field coming from the transformer.

Question here: How to disable ValueToDuplicatesTransformer errors when the validation group is not person ?

+6
source share
3 answers

Since buildForm skips the code associated with adding fields, I assume:

  • you do not data_class form to the object via data_class directly, but set the values ​​in your controller
  • The type repeated has required => true as a parameter and must check the password
  • $data passed to formbuilder contains the value for the repeated field.

Basically the problem is that validation is triggered. If there is any data, then the type will always perform basic checks (the repeated field must have the same value twice).

What you have to do to solve this, do not pass the value of $data in the first place and make sure required => false for the repeated field. After that, your form will only check the group, as you have already explained.

Perhaps the most reliable solution is different, and it would be better to make two FormTypes instead of using groups . This will eliminate most of the complexity that you describe in your question.

+2
source

You really cannot deactivate errors from the DataTransformer, these errors are added by FormValidator , and this behavior is not customizable. This is quite logical, since the conversion errors lead to the fact that these models are in a broken state.

What would I do in this case, use field 2 instead of a repeating field with a second with mapped => false and an event listener, which adds an error manually when necessary.

0
source

Maybe I'm wrong, but the validation team is not the right tool to solve the problem at all. The dependence of the validation rules that you want to solve is based on data, while the verification group is not about that, it is about the same data being checked differently depending on the context.

If you want to apply some rules only for a person (or, conversely, only for a company), I would prefer to perform a random check in Client Entity

 namespace AppBundle\Entity; class Client { .... /** * @Assert\IsTrue(message="Person should have a valid Title") */ public function isPersonTitleValid() { return $this->type != Client::TYPE_PERSON || $this->isTitleValid(); } public function isTitleValid() { // Here implement your validation applicable only for person; } 
0
source

All Articles