Search code examples
phpformssymfonybundle

Symfony 2 - multiple forms


Does anybody know how to render more forms in one page?

I have an entity class ImportPath: id. description, path, local and ImportPathForm corresponding to that entity. What I want is somethnig like table, which has in each row little form, that can edit one Path in it.

I don´t know the final count of path, so it must be dynamic in some loop. The reqested form should be known from path id (not implemented yet).

Code: Controller: 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));
}

View (extract):

{% 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 %}

EDIT>

I get it :)

the solution is

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

Solution

  • I get it :)

    the solution is

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