Removing ChoiceType transformers during Pre Submit events

608 Views Asked by At

I have a form with a collection subform inside. In the subform, there is a choiceType field for the attribute "resourceId". Its data are populated by ajax, with Select2 js plugin (because its data depends on another choice, in which you select the resource type).

In the specific case that i have already a value in resourceId choice, i can't validate my field :

transformationFailure: TransformationFailedException {#4328 ▼
  #message: "Unable to reverse value for property path "resourceId": The choice "bd922d35fb828da6e39edf3c7927511c9a6be025" does not exist or is not unique"

This is due because i have to add the default value of the field by javascript (thanks Select2).

I need to cancel the validation on the field, but even if i use the ResetViewTransformers() in the BuildForm method and by rebuilding the field in the PreSubmit event, it still doesn't validate.

TL;DR : How can i cancel validation during PreSubmit event ? (only on my field if possible)

2

There are 2 best solutions below

0
On BEST ANSWER

Found the solution. I was going in the wrong direction.

All i had to do was overriding the ChoiceType class and disable the ViewTransformers, then use the new class :

    class NonTransformedChoiceType extends ChoiceType
   {
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder->resetModelTransformers();
        $builder->resetViewTransformers();
    }
}
1
On

add some jquery and add the .ignore class to your specific form field. If you load whole form i suggest loading them seperately so that way you can easily add it.

$("#myform").validate({
   ignore: ".ignore, :hidden"
})

Hidden will also make sure that any fields you do hide, for other reason will not be affacted and then give errors, reference: here