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

标签 php symfony twig symfony-forms

我正在使用 symfony2。我正在尝试覆盖 Twig 中的默认 div 样式表单 block 。

首先,是否有人知道或知道字段集和列表(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.twig 的模板,它使用 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 %}

关于php - 创建 Symfony2 表单主题 - 字段集和列表样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7457473/

相关文章:

php - 如何在 Symfony 4 中使用自定义集合

php - 如何在 Drupal 8 的搜索结果页面上显示结果计数?

php - 从 JOIN 查询检索行时缺少列数据

java - 如何在tomcat和php之间传递 session

php - Laravel - 默剧 :pdf not working

PHP代码注释解析速度

php websocket Ratchet 不从 Doctrine 中获取最新数据

phpunit + symfony 错误处理程序已更改

javascript - 按下按钮后添加动态表单字段

symfony - Silex + Twig : form_widget() not found, 即使安装了 Symfony Bridge