I create a simple form with multiple rows:
Controller:
public function indexAction()
{
$repository = $this->getDoctrine()->getRepository('MyBundle:Product');
$products = $repository->findAll();
foreach ($products as $product) {
$forms[] = $this->createForm(new ProductType, $product)->createView();
}
return $this->render('MBundle:Default:index.html.twig', array('form' => $forms);
}
I render this in a twig:
<form action="{{ path('_submit') }}" method="post">
{% for key, formData in forms %}
{{ form_row(formData.id) }}
{{ form_row(formData.name) }}
{{ form_row(formData.nameEnglish) }}
<br clear="all" />
{% endfor %}
</form>
When i submit form each of my input field set has the same name attributes and i get only the last one. How to grab all the rows and validate them in my submitAction() controller? Each input needs to have unique name, right? … and perhaps i need to set somehow name=”something[name][]” but how to do it?
Ok Cerad was right with his comment and we have to use collection for this. It may sound like a nonsense at first but it kinda is right. It took me a while to get head around it.
So i had to create a ProductsType which is an arrayCollection and inserts each Product. (just like in documentation with Task and tags)
I used that:
Then in twig i do:
Job done.
And even you can apply themes to each row by this:
Cool stuff!