创建一个Symfony2表单主题-字段集和列表样式


Creating a Symfony2 Form Theme - Fieldset and List style

我使用symfony2。我试图在树枝中覆盖默认的div样式表单块。

首先,是否有或知道一个可用的实现字段集和列表(ul -> li)的方法?

目前,我实现了这样的字段集支持:

类型:

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')),
                    ));
}

我有一个名为fieldset.html的模板。小枝,它使用视图的属性:

{% 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 %}

下面是一个简单的字段集示例:https://gist.github.com/spcmky/8512371

要用列表替换div,请查看form_widget_compound和form_rows。您可以:

{% 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 %}

缺省情况下,Twig在呈现表单时使用div布局。但是,您可以在表格布局中呈现表单。使用form_table_layout.html。使用这种布局的小枝资源:

# app/config/config.yml
twig:
    form:
        resources: ['form_table_layout.html.twig']

http://symfony.com/doc/2.0/cookbook/form/form_customization.html

我不知道这些的实现,但欢迎您制作它们并打开拉请求。