Symfony2 - How to create a form field of type "entity" without values

I have a form with an EntityType field. The table from which the values ​​were taken has grown, and the selection window that is displayed makes the page large (= slow loading).

I replaced this:

  ->add( 'contact', 'entity', array( 'class' => 'CRMCoreBundle:Contact', 'required' => false, 'empty_data' => null, ) ) 

with:

  ->add( 'contact', 'entity', array( 'class' => 'CRMCoreBundle:Contact', 'choices' => array(), 'required' => false, 'empty_data' => null, ) ) 

to display an empty selectbox, and on the interface side I use AJAX to populate and autocomplete selectbox.

The problem is that now when I submit the form, it is not valid. Any ideas?

+1
source share
1 answer

It does not pass validation because the values ​​you submit were not added by the form component when creating the form. This is necessary to protect the form from accepting unauthorized values.

The proper way to do this is for your ajax to request a form to update the select box on the server using form events, and then update the displayed selection with the correct values.

Read more about form events here - http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

+3
source

All Articles