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

时间:2011-09-17 20:11:30

标签: php symfony twig symfony-forms

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

首先,有没有或知道fieldset和list(ul - > li)方法的可用实现?

4 个答案:

答案 0 :(得分:3)

目前,我实现了这样的fieldset支持:

中的类型:

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.twig的模板,它使用视图的属性:

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

答案 1 :(得分:2)

这是一个简单的字段集示例: 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 %}

答案 2 :(得分:1)

默认情况下,Twig在渲染表单时使用div布局。但是,您可以在表格布局中渲染表单。使用form_table_layout.html.twig资源来使用这样的布局:

# app/config/config.yml

twig:
    form:
        resources: ['form_table_layout.html.twig']

答案 3 :(得分:0)

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

我不知道thoose的实现,但欢迎你制作并打开Pull Request。