Home > Software engineering >  How to disable symfony validation of a non mapped field
How to disable symfony validation of a non mapped field

Time:09-17

I have an error message "This value is not valid." on a non mapped field. This field is a dropdown with a maximum of 20 items. The user can load more results using ajax. The error happens only when I choose a value loaded using ajax. If I use an element from the original list that shows when the page loads, I don't have this error. I understand that symfony considers it a non valid value because it is not in the original query result, but that's not what I want. Here is the field definition in the form type:

    ->add('clientcustomer', EntityType::class, array(
        'label' => 'Customer',
        'class' => Customer::class,
        'choices' => $this->customerRepository->findClientCustomersForSelect2(),
        'placeholder' => 'New customer',
        'required' => false,
        'mapped' => false,
    ))

I would like to disable validation for this field.

CodePudding user response:

You can use FORM EVENTS see documentation Form-Events

Use PRE_SUBMIT event to update your dropdown choices based on form submitted values

CodePudding user response:

You can use 'validation_groups' => false,

This will prevent validation on your unmapped field.

See validation_groups

So for your property:

->add('clientcustomer', EntityType::class, array(
    'label' => 'Customer',
    'class' => Customer::class,
    'choices' => $this->customerRepository->findClientCustomersForSelect2(),
    'placeholder' => 'New customer',
    'validation_groups' => false,
    'required' => false,
    'mapped' => false,
))
  • Related