Symfony 2 - 多个表单

4
有人知道如何在一页中渲染更多的表单吗?
我有一个实体类ImportPath,其中包括id、description、path和local,以及相应的ImportPathForm。 我想要的是类似于表格的东西,在每一行都有一个小表单,可以编辑其中的一个Path。
我不知道路径的最终数量,因此它必须在某个循环中动态生成。 请求的表单应该根据路径id进行识别(尚未实现)。
代码: 控制器: public function importAction($message="no message") {
$em = $this->getDoctrine()->getEntityManager();

$paths = $em->getRepository('WT2\BabuBundle\Entity\ImportPath')->findAll();

$forms=array();
foreach ($paths as $path) {
$form = $this->createForm(new ImportPathForm(), $path);
$forms[]=$form;
}

// $request = $this->getRequest();
// if ($request->getMethod() == 'POST') {
// $form->bindRequest($request);
// if ($form->isValid()) {
// /* ok */
// }
// }

return $this->render('WT2BabuBundle:Admin:import.html.twig', array('forms'=>$forms,'message'=>$message));
}

视图(提取):

{% for key, form in forms %}
{{ key }}
<form action="{{ path('admin_import') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" value="Ym2nit" />
</form>
{% endfor %}

编辑>

我明白了 :)

解决方案是

$forms=array();
foreach ($paths as $path) {
$form = $this->createForm(new ImportPathForm(), $path);
**$form = $form->createView();**
$forms[]=$form;
}

我知道这个网站的运作方式,但是我的声望不到100,所以在提问后8小时内无法回答自己的问题... - Jan Bouchner
没问题,现在我可以在答案框中发布解决方案 :) - Jan Bouchner
1个回答

5
我明白了 :)
解决方案是:
$forms=array();
foreach ($paths as $path) {
$form = $this->createForm(new ImportPathForm(), $path);
$form = $form->createView();
$forms[]=$form;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接