简体   繁体   English

symfony2-如何创建没有值的“实体”类型的表单域

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

I have a Form with an EntityType field. 我有一个带有EntityType字段的表单。 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. 渲染一个空的选择框,在前端,我使用AJAX填充并自动完成选择框。

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. 正确的方法是让ajax请求表单使用表单事件更新后端的select字段,然后使用适当的值更新显示的select。

More on form events here - http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html 有关表单事件的更多信息-http: //symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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