symfony2 - How to create a form field of type “entity” without values

亡梦爱人 提交于 2020-01-13 19:34:48

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/26469536/symfony2-how-to-create-a-form-field-of-type-entity-without-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!