Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Symfony2 Form Theme - Fieldset and List style

I am using symfony2. I am trying to override the default div style form blocks in twig.

First, does any have or know of an available implementation of the fieldset and list (ul -> li) approach?

like image 363
Jeremy Avatar asked Sep 17 '11 20:09

Jeremy


2 Answers

For the moment, I implemented fieldset support like this:

in the Type:

public function buildView(FormView $view, FormInterface $form, array $options)
{
    $view->setAttribute('fieldsets',
            array(
                array(
                    'legend' => 'film.group.date',
                    'content'=> array(
                        'theaters_release_date',
                        'storage_media_release',
                        'storage_media_release_date',
                        'vod_release_date'
                        )),
                array(
                    'legend' => 'film.group.country',
                    'content'=> array('countries')),
                    ));
}

I have a template named fieldset.html.twig, that uses the attributes of the view:

{% macro fieldset_block(fieldset, form) %}
<fieldset{% if fieldset.subform is defined %} class="{{ fieldset.subform }}"{% endif %}>
    <legend>{{fieldset.legend | trans }}</legend>
    {% if fieldset.content is defined%}
      {% for row in fieldset.content %}
          {{ form_row(form[row]) }}
      {% endfor %}
    {% endif %}
    {% if fieldset.subform is defined %}
        {# Couldn't get some recursivity (simply call form widget) here... too bad #}
        {% if form[fieldset.subform].get('attr').fieldsets is defined %}
            {% for subfieldset in form[fieldset.subform].get('attr').fieldsets %}
                {{ _self.fieldset_block(subfieldset, form[fieldset.subform]) }}
            {% endfor %}
        {% else %}
            {% for row in form[fieldset.subform] %}
                {{ form_row(row) }}
            {% endfor %}
        {% endif %}
    {% endif %}
    {% if fieldset.items is defined%}
      {% for fieldset in fieldset.items %}
          {{ _self.fieldset_block(fieldset, form) }}
      {% endfor %}
    {% endif %}
</fieldset>
{%  endmacro %}

{% block form_widget %}
    {% for fieldset in form.get('attr').fieldsets %}
        {{ _self.fieldset_block(fieldset, form) }}
    {% endfor %}
{% endblock %}
like image 115
greg0ire Avatar answered Oct 30 '22 14:10

greg0ire


Here is a simple fieldset example: https://gist.github.com/spcmky/8512371

To replace the divs with a list, look at form_widget_compound and form_rows. You can:

{% block fieldset_widget %}
    {% spaceless %}
        <fieldset {{ block('widget_container_attributes') }}>
            {% if title is defined %}<legend>{{ title }}</legend>{% endif %}
                <ul>
                {% for child in form %}
                    <li>
                        {{ form_widget(child) }}
                    </li>
                {% endfor %}
                </ul>
        </fieldset>
    {% endspaceless %}
{% endblock %}
like image 41
Chris Edwards Avatar answered Oct 30 '22 14:10

Chris Edwards