Symfony2。如何允许窗体忽略额外字段? [英] Symfony2. How to allow form to ignore extra fields?

查看:221
本文介绍了Symfony2。如何允许窗体忽略额外字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一些数据发布到我的Symfony表单,并使用 submit()方法。

I need to post some data to my Symfony form and I use submit() method for this.

但是当请求包含一些额外的数据时,我遇到了错误:

But when request contains some extra data, I got error:


此表单不应包含额外的字段

"This form should not contain extra fields"

是否可以让表单缺少使用表单配置的额外数据?

也许还有另一种方法可以做到这一点?

注意: submit()方法,而不是 handleRequest(),因为我通过跨域ajax请求发布我的数据,所以form不能以这种方式提交。原因: isValid() return false ,因为 isSubmitted() return false。

Notice: I need submit() method, not handleRequest() because I post my data through cross-domain ajax request, so form cannot be submitted this way. Reason: isValid() return false, because isSubmitted() return false.

PS 当然,我可以创建服务来处理我的请求,然后传递给表单,

P.S. Of course, I can create service that will be handle my request before passing it to the form, but maybe more elegant way exists.

推荐答案

在您的表单类型中:

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\YourEntity',
            'translation_domain' => strtolower('entity_translation_domain'),
            'allow_extra_fields' => true,
        ));
    }

allow_extra_fields 允许您的表单接收额外的数据

The allow_extra_fields will enable your form to receive the extra data

这篇关于Symfony2。如何允许窗体忽略额外字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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