Create a form with a for in Symfony

I created a form in Symfony like this:

$form = $this->createFormBuilder($template)
    ->add('product1', 'text')
    ->add('product2', 'text')
    ->add('save', 'submit')
    ->getForm();

Now this is my twig:

{{ form_start(form) }}
   {% for i in 1..2 %}
      <div class="col-md-3">
         <div class="product">
            <div class="name">
               {{ form_label(form.product{{ i }} ) }}
               {{ form_errors(form.product{{ i }} ) }}
               {{ form_widget(form.product{{ i }} ) }}
            </div>
         </div>
      </div>
   {% endfor %}
{{ form_end(form) }

The main idea is iterate over the for and get a new form.product<X> each loop.
I can’t make it works and I don’t even know if it can be done in this way. Any idea?


Source: forms

Leave a Reply