symfony2: Applying theme to individual field for collection type
symfony, symfony-forms
Solution
As of Symfony 2.1 (it may work as well for 2.0 but not sure) to apply theme for collection you do this way:
Lets say we have a collection of products (we have multiple Product entities)
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 theme can 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 trick is with "entry" where twig will do the job for you to apply above changes to each row and for each field you specify
Hope that helps!
Problem
Just applying a theme to form field is easy e.g.: ``` {% form_theme form _self %} {% block _product_name_widget %} {{ block('field_widget') }} {% endblock %} ``` but what if the form field is of collection type? E.g.: `product['models'][1][comment`, I have no ideas how to customize it. (This may be the first kind question here) UPDATE: anwser here: Symfony2: collection form field type with data-prototype