Symfony Twig Form 特定字段的主题字段、主题、Symfony、Twig

2023-09-07 00:27:29 作者:落ー地心碎〞没人心疼

我有一个自定义表单字段类型和一个关联的表单主题.在一页上我有很多这样的字段,但我特别想更改其中一个字段.

有什么方法可以对同一类型(以及在同一文件中)的某些字段进行不同的主题化吗?

限制安全角色访问特定字段

一个简化的例子:

form_fields.html.twig:(本地主题文件)

{% block my_dropdown_row %}

{{ form_label(form) }}{{ form_widget(form) }}{{ form_errors(form) }}</div>{% 端块 %}

在我的表单模板中(所有这些字段都具有相同的类型 - my_dropdown

{{ form_row(form.selectionA) }}{{ form_row(form.selectionB) }}{{ form_row(form.selectionC) }}{{ form_row(form.final_selection) }}

如何以不同于其他字段的方式设置最终字段的样式?这些小部件中有很多代码,因此重复越少越好.

解决方案

这个是可以的.方法如下:

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

I have a custom form field type and an associated form theme for it. On one page I have a lot of these fields, but one of the fields in particular I want to change.

Is there any way to theme certain fields of the same type (and in the same file) differently?

A simplified example:

form_fields.html.twig: (local theming file)

{% block my_dropdown_row %}
<div>
    {{ form_label(form) }}
    {{ form_widget(form) }}
    {{ form_errors(form) }}
</div>
{% endblock %}

In my form template (all these fields have the same type - my_dropdown

{{ form_row(form.selectionA) }}
{{ form_row(form.selectionB) }}
{{ form_row(form.selectionC) }}
{{ form_row(form.final_selection) }}

How can I style the final field differently to the others? There is a lot of code in these widgets so less duplication the better.

解决方案

This can be done. Here is how:

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

 
精彩推荐