简体   繁体   中英

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 are taken has grown, and the select box that is rendered makes the page to big (=slow to load).

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 render an empty selectbox, and on the frontend side I use AJAX to populate and autocomplete the selectbox.

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

It does not pass validation because the values you are submitting were not added by form component when the form was created. This is to protect the form from accepting unauthorized values.

The proper way to do that is to have your ajax request the form to update the select field on backend using form events, and then update the displayed select with proper values.

More on form events here - http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM