luxury
luxury

Reputation: 159

symfony2: Applying theme to individual field for collection type

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

Upvotes: 4

Views: 4023

Answers (3)

Ryan
Ryan

Reputation: 5046

You can override the collection_widget theme for a single field by referencing the widget like this as well.

For example, if "categories" is a "collection" widget on a ProductType form, you can do this.

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

Upvotes: 0

TroodoN-Mike
TroodoN-Mike

Reputation: 16185

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!

Upvotes: 5

Anil Gupta
Anil Gupta

Reputation: 2409

You can do with override 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 how to customize form collection type try to look at this Product Bundle

Upvotes: 1

Related Questions