Symfony2通过一个提交按钮发送多个表单

时间:2014-06-03 09:45:56

标签: forms symfony

我需要帮助。我创建了视图,我正在为一种类型的对象创建多个表单。现在我想保存表单,只需一个按钮,然后将这些对象保存到数据库。

这是控制器:

/**
 * @Route("/project/{project_id}/string/{id}/edit/", name="StringEdit") 
 * @Template()
 */
public function editAction($project_id, $id, Request $request)
{

    $string = $this->getDoctrine()->getRepository('DomestosTranslatingBundle:String')->find($id);
    $translations = $this->getDoctrine()->getRepository('DomestosTranslatingBundle:Translation')->findByString($string);

    //$form = $this->createForm(new TranslationType(), $translation);

    //$form->handleRequest($request);

    $forms = array();
    foreach($translations as $translation){
        $form = $this->createForm(new TranslationType, $translation);
        $form = $form->createView();
        $forms[] = $form;
    }

    return $this->render('DomestosTranslatingBundle:String:edit.html.twig', array(
        'forms' => $forms,
        'string' => $string,
        )); 
}

观点:

{% extends "::base.html.twig" %}

{% block title %}Edit translations{% endblock %}

{% block body %}
Code: {{string.code}}
<p>


<table> 
{% for keylang,lang in string.project.lang %}
    {% for key,form in forms %}
        {% if key == keylang %}
            <tr>
                <td>{{lang.title}}</td>
                <td>{{form_widget(form.text)}}</td>
            </tr>
        {% endif %}
    {% endfor %}
{% endfor %}
</table>

<p>

{% endblock %}

1 个答案:

答案 0 :(得分:4)

不,你不能这样做。一次只能提交一份表格。

您可以创建一个表单并 Embed a Collection of Forms ,而不是使用表单数组。