Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable backend validation for choice field in Symfony 2 Type

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.

like image 579
hywak Avatar asked Dec 30 '14 13:12

hywak


2 Answers

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).

like image 173
Atan Avatar answered Sep 20 '22 11:09

Atan


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

like image 28
hywak Avatar answered Sep 23 '22 11:09

hywak