【问题标题】:Customizing the form theme of a form with a CollectionType使用 CollectionType 自定义表单的表单主题
【发布时间】:2016-09-12 14:56:17
【问题描述】:

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

在我的 config.yml 中指定了一个全局表单主题。

例如这个:

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

如果我在我的树枝模板中设置一个像这样的不同表单主题...

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

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

我该如何解决这个问题?

【问题讨论】:

    标签: php forms symfony symfony-forms


    【解决方案1】:

    不确定这是否是您要查找的内容,但我不将 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="answer-template" type="text/template">{{ form_widget(form.answer.vars.prototype) }}</script>
    

    这就是我加载主题的方式(这只是 wip 模板中的一个示例,尚未移动模板文件)

    {% form_theme form _self %}
    

    我每次都手动选择我的表单主题,而不是使用全局配置,因为在某些时候我遇到了一些问题并且没有留下来解决它.. 与我得到双 div 包装的 crud 操作相关的问题一个来自原型中我自己的 html,第二个来自表单主题,这就是我手动渲染每个字段的原因

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 2021-08-29
      • 2014-10-17
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多