使用CollectionType自定义表单的表单主题


Customizing the form theme of a form with a CollectionType

我正在使用symfony,我渲染一个使用集合类型的表单。

在我的配置。指定全局表单主题

例如:

twig:
debug:            "%kernel.debug%"
strict_variables: "%kernel.debug%"
form_themes: 
    - 'foundation_5_layout.html.twig'

如果我在我的twig模板中设置一个不同的表单主题,像这样…

{% form_theme form 'Form/bootstrap_3_layout.html.twig' %}

这对我所有的其他表单元素都有影响,但对类型为CollectionType的元素没有影响。它仍然使用config.yml

中定义的主题。

如何解决这个问题?

不确定这是否是你要找的,但我不使用配置。我在每个文件上手动设置表单主题,并根据我想要的表单外观手动渲染所有内容。

输入字段的示例(表单类中的名称是answer

)
{% block _suggest_question_manager_answer_entry_widget %}
    {% spaceless %}
        <div class="multiple-choice-container">
            <div class="row form-group">
                {{ form_label(form.value, null, {'label_attr': {'class': 'col-sm-2 control-label'} } ) }}
                <div class="col-md-12">
                    <div class="input-group">
                        <div class="input-group-addon">
                            <span class="glyphicon glyphicon-record" aria-hidden="true"></span>
                        </div>
                        {{ form_widget(form.value, {'attr': {'class': 'form-control'} } ) }}
                        {{ form_errors(form.value) }}
                        <div class="input-group-btn">
                            <button class="add-answer btn btn-default" aria-label="Add Choice">
                                <span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span>
                            </button>
                            <button class="remove-answer btn btn-default" aria-label="Remove Choice">
                                <span class="glyphicon glyphicon glyphicon-minus" aria-hidden="true"></span>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    {% endspaceless %}
{% endblock %}

如何渲染原型

<script id="answerswer-template" type="text/template">{{ form_widget(form.answer.vars.prototype) }}</script>

这就是我加载主题的方式(这只是一个来自wip模板的例子,还没有移动模板文件)

{% form_theme form _self %}

我每次手动选择我的表单主题,而不是使用全局配置一个,因为在某些时候问题我有一些问题,没有留下来弄清楚。可能与crud操作有关我在原型中获得双div包装一个来自我自己的HTML第二个来自表单主题这就是为什么我手动呈现每个字段