symfony2 - 如何创建一个类型为“entity”的表单字段没有价值 [英] symfony2 - How to create a form field of type "entity" without values

查看:127
本文介绍了symfony2 - 如何创建一个类型为“entity”的表单字段没有价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 EntityType 字段的表单。从中获取值的表已增加,并且呈现的选择框使页面变大(=加载速度缓慢)。

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

渲染一个空的选择框,并在fron我倾向于使用AJAX来填充和自动完成选择框。

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.

正确的方法是让您的ajax请求表单,以使用表单事件更新后端的选择字段,然后用适当的值更新显示的选择。

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.

更多关于表单事件的信息 - http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

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

这篇关于symfony2 - 如何创建一个类型为“entity”的表单字段没有价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆