Symfony2:同一页面中的两个表单 [英] Symfony2 : Two forms in a same page

查看:35
本文介绍了Symfony2:同一页面中的两个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个页面中有两个表单.

我的问题是,当我尝试提交表单时,它就像尝试提交页面下方的第二个表单一样.

如下,你可以找到我的两个表格:

公共函数 createSuiviForm() {返回 $form = $this->createFormBuilder(null)->add('numero', 'text', array('label' => 'N° : ','约束' =>大批(new AssertNotBlank(array('message' => 'XXXX')),new AssertLength(array('min' => 19, 'max' => 19, 'exactMessage' => 'XXX {{ limit }} XXX')))))->add('xxxx', '提交')->getForm();}公共函数 createModificationForm() {返回 $form = $this->createFormBuilder(null)->add('修改', '提交', array('label' => 'XXXXXXXXXXXXXXXXXXXXX'))->getForm();}

我的第二个表单只是一个提交按钮.

我将它们传递给我的渲染并使用:

<form method="post" action='' {{form_enctype(form)}} >{{ form_widget(form) }}<input type="submit" class="btn btn-primary"/></表单><div class='errors'>{{ form_errors(form) }}

'form' 是第一个表单的变量名并为我的第二个表单更新".

当我尝试提交第二个表单时,我需要单击两次,最后我得到:

此表单不应包含额外的字段."以及其余表单的所有无效输入.

我尝试将validation_group添加到false但无济于事.

我不明白为什么我会收到这个错误,因为我的表单根本没有嵌入

希望你能理解...

解决方案

你必须分别对待表单:

if('POST' === $request->getMethod()) {if ($request->request->has('form1name')) {//处理第一个表单}如果 ($request->request->has('form2name')) {//处理第二种形式}}

这在 Symfony2 中得到了完美的解释多个表单:不同于嵌入表单(暂时不可用 - 见下文)

更新

由于上面提供的链接暂时不可用,您可以查看该资源的存档 此处.

I've got two forms in a same page.

My problem is when I tried to submit a form, it's like it tried to submit the second form below in the page as well.

As follow, you can find my 2 forms :

public function createSuiviForm() {

    return $form = $this->createFormBuilder(null)
            ->add('numero', 'text', array('label' => 'N° : ',
                'constraints' => array(
                    new AssertNotBlank(array('message' => 'XXXX')),
                    new AssertLength(array('min' => 19, 'max' => 19, 'exactMessage' => 'XXX {{ limit }} XXX')))))
            ->add('xxxx', 'submit')
            ->getForm();
}

public function createModificationForm() {

    return $form = $this->createFormBuilder(null)
            ->add('modification', 'submit', array('label' => 'XXXXXXXXXXXXXXXXXXXX'))
            ->getForm();
}

My second form as only a submit button.

I passed them to my render and display them by using :

<div class="well">
    <form method="post" action='' {{form_enctype(form)}} >
        {{ form_widget(form) }}
        <input type="submit" class="btn btn-primary"/>
    </form>
    <div class='errors'>
        {{ form_errors(form) }}
     </div>
</div>

'form' is the name of my variable to the first form and 'update' for my second form.

When I attempted to submit my second form, I need to click twice and finally I get :

"This form should not contain extra fields."
And all non valid input for the remainding form.

I tried to add validation_group to false but to no avail.

I don't understand why I got this error because my forms are not embedded at all

I hope you will understand...

解决方案

You have to treat the forms separately:

if('POST' === $request->getMethod()) {
 
    if ($request->request->has('form1name')) {
        // handle the first form  
    }

    if ($request->request->has('form2name')) {
        // handle the second form  
    }
}

This is perfectly explained in Symfony2 Multiple Forms: Different From Embedded Forms (temporarily unavailable - see below)

Update

As the link provided above is temporarily unavailable, you can see an archive of that resource here.

这篇关于Symfony2:同一页面中的两个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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