Symfony2: applying a theme to a single field for a collection type

Just applying a theme to create a field is easy, for example:

{% form_theme form _self %} {% block _product_name_widget %} {{ block('field_widget') }} {% endblock %} 

but what if the form field has a collection type? For example: product['models'][1][comment , I have no idea how to configure it. (This may be a question of the first kind)

UPDATE: anwser here: Symfony2: collection form field type with data prototype

+4
source share
3 answers

As with Symfony 2.1 (it can work for 2.0, but not sure) to apply the theme to the collection, you do this:

Let's say we have a set of products (we have several Product objects)

Controller:

  $repository = $this->getDoctrine()->getRepository('ExampleBundle:Product'); $products = $repository->findAll(); $productCollection = new Products; foreach ($products as $product) { $productCollection->getProducts()->add($product); } $collection = $this->createForm(new ProductsType, $productCollection); return $this->render('ExampleBundle:Default:index.html.twig', array( 'collection' => $collection->createView() )); 

Your topic may look like this:

 {% block _productsType_products_entry_name_row %} <div class="yourDivName">{{ block('form_widget') }}</div> {% endblock %} {% block _productsType_products_entry_description_row %} <div class="yourDivDescription">{{ block('form_widget') }}</div> {% endblock %} 

The โ€œinputโ€ trick, where the branch will complete the task, to apply the above changes to each row and for each field that you specify

Hope this helps!

+5
source

You can override the collection_widget block:

 {% block collection_widget %} {% spaceless %} {% if prototype is defined %} {% set attr = attr|merge({'data-prototype': form_row(prototype) }) %} {% endif %} {{ block('form_widget') }} {% endspaceless %} {% endblock collection_widget %} 

If you want to customize the type of form collection, try looking at the Product Bundle.

+1
source

You can override the collection_widget theme for one field by specifying such widgets as well.

For example, if the โ€œcategoryโ€ is a โ€œcollectorโ€ widget in the form of a ProductType , you can do this.

 {% block _product_categories_widget %} {% for child in form %} {{- form_row(child) -}} <hr> {% endfor %} {% endblock %} 
0
source

All Articles