Symfony 2 - 多种形式


Symfony 2 - multiple forms

有人知道如何在一个页面中呈现更多表单吗?

我有一个实体类导入路径:id.描述,路径,本地和与该实体对应的 ImportPathForm。我想要的是一些类似表格的东西,它在每行中都有小形式,可以编辑其中的一个路径。

我不知道路径的最终计数,所以它在某个循环中必须是动态的。请求的形式应该从路径 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;
}

我明白了:)

解决方案是

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