Disable backend validation for choice field in Symfony 2 Type

16,982

Solution 1

I found the solution

Symfony2.4 form 'This form should not contain extra fields' error

For more details: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-dynamic-form-modification-suppressing-form-validation

Solution 2

It's confusing but this behaviour is not really validation related as it is caused by the "ChoiceToValueTransformer" which indeed searches for entries in your pre-declared list. If your list is empty or you want to disable the transformer you can simply reset it.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('yourField', 'choice', array('required'=>false));

    //more fields...

    $builder->get('yourField')->resetViewTransformers();
}

Then your custom defined validation will step in (if it exists).

Share:
16,982

Related videos on Youtube

hywak
Author by

hywak

Updated on July 14, 2022

Comments

  • hywak
    hywak almost 2 years

    Is it possible to disable backend (server-side) validation for the specified field?

    Wnen Im trying to send form with dynamicly loaded options I get error "ERROR: This value is not valid."

    I think symfony is checking if my value is on the default declared list (in my case its empty list), if not returns false.

  • fabpico
    fabpico over 8 years
    Your solution had no effect for me in symfony 2.8. But this helped: stackoverflow.com/questions/12946461/…
  • Tom
    Tom almost 8 years
    If I use the resetViewTransformers option it works, only when the form is reloaded because an other field gave an error the choice value is lost. Do you have an idee how to fix this?
  • simohe
    simohe about 6 years
    The remove() call is not necessary. add() replaces an existing field.
  • Volmarg Reiso
    Volmarg Reiso over 4 years
    This saved me so badly in symfony 4.2 after couple of hours of search. Works perfectly. In my case I render fields in twig in the form type template, which is not equal to what i pass in. I could not force isValid(), and kept getting invalid value.