0
votes

How do you copy a form array built using form builder in symfony 2 at the twig level?

I have the form group below, which creates a multi-layered form

 {{ form_row(form.products.ItemOptions) }}

I was hoping to copy this array form.products.Options into new array in twig so i can display and edit it in another section on the same page.

I have tried using twigs merge function, and also jquery. Any help would be greatly appreciated

3
"I have tried using twigs merge function"... can you show us that? Generally speaking, need to merge arrays in your twig usually (not always) points to a bad implementation decision somewhere along the road...Jovan Perovic

3 Answers

0
votes

I think you can just use set for that.

{% set productOptions = form.products.Options %}

Note that you can also customize the view of your forms via AbstractType::buildView()

0
votes

the form that is rendered uses an Entity. (e.g. Product). Since you already have an instance of that entity in your controller you could simply add this entity to your Twig data. return $this->render('default/new.html.twig', array( 'form' => $form->createView(), 'products' => $products, )); }

In twig you can simply do something like:

{% for product in products %}
    {% for itemOption in product.itemOptions %}
        {{ itemOption }}<br>
    {% endfor %}
{% endfor %}

Or you make a second formType class and generate a second form in your controller using the new formType and add your form to you Twig data.

0
votes

I ended up solving my issue with simple jquery if it helps anyone. By using appendTO() command i was able to strip out rows on the dom and reorganise them. Also using $( "#divID" ).clone().appendTo( "#NewPosition" ); allowed me to display the form in two locations, although as the properties are also copied it wasn't the most functional method.