I have a list of "hours" that workers passed on mandates. An admin can check them (checkbox)to add them to a bill, (but he can adapt the real hours)
So, I have an array of hour selected, in my controler I get the hours :
$hours = $repository->findBy(array('id' => $tabHour));
so $hours
countain more than one hour, I thought that if I was creating a form with $hours, it will automaticly display the fields more than once..
foreach($hours as $key => $test){
$billedHour[$key] = new BilledHour();
$form[$key] = $this->container->get('form.factory')->create(new BillMandateForm(), $hour);
}
I tried to do like this. But it's not solved, cause if I return a collection of forms I can't do 'form' => $form->createView()
so I can't render the forms...
Try to pack all your form views in array
$billedHours = array();
$forms = array();
foreach($hours as $hour) {
$billedHours[] = new BilledHour();
$forms[] = $this->container->get('form.factory')->create(new BillMandateForm(), $hour);
}
Then, create array of rendered form views
$formsToRender = array();
foreach($forms as $form) {
$formsToRender[] = $form->createView();
}
Then, pass this array to Twig:
...
'forms' => $formsToRender
...
And finally in Twig render each of form views from array:
{% for form in forms %}
{{ form_row(form.id) }} {# or form_widget(form) if you want to render whole from instead of some fields #}
{% endfor %}
I hope this helps ;)