问题描述
例如
我在表单对象中有两个成员.
For example
I have two members in form object.
form_widget(form.icon)
form_widget(form.icon)
form_widget(form.name)
form_widget(form.name)
我更改了"choice_widget_expanded"
I have changed 'choice_widget_expanded'
{% block choice_widget_expanded %}
{% spaceless %}
<table {{ block('widget_container_attributes') }}>
{% for child in form %}
<tr>
{{ form_widget(child) }}
{{ form_label(child) }}
</tr>
{% endfor %}
</table>
{% endspaceless %}
{% endblock choice_widget_expanded %}
但是我只想让它影响{{form.icon}}
However I would like to make it affect on {{form.icon}} only
有可能吗?我怎么知道传递给该块的对象是form.icon或form.name?
is it possible ? how can I tell the object passed to this block is form.icon or form.name?
推荐答案
要覆盖choice_widget_expanded
的标签块,您可以定义块并按以下方式使用它
To override label block for choice_widget_expanded
you can define your block and use it like in below
{% block choice_widget_expanded %}
{% spaceless %}
<table {{ block('widget_container_attributes') }}>
{% for child in form %}
<tr>
{{ form_widget(child) }}
{{ form_label_custom(child) }}
</tr>
{% endfor %}
</table>
{% endspaceless %}
{% endblock choice_widget_expanded %}
对于自定义标签也是如此form_label_custom
And for the custom label too form_label_custom
{% block form_label_custom %}
{% spaceless %}
{% if label is not sameas(false) %}
{% if not compound %}
{% set label_attr = label_attr|merge({'for': id}) %} /* you can skip this part for td */
{% endif %}
{% if required %}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{% endif %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
<td {% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</td>
{% endif %}
{% endspaceless %}
{% endblock form_label_custom %}
甚至更多,您可以定义自定义form_widget_custom(child)
块以覆盖
Or even more you can define the custom form_widget_custom(child)
block to override like
{% block form_widget_custom %}
{% spaceless %}
{% if compound %}
{{ block('form_widget_compound') }}
{% else %}
{{ block('form_widget_simple') }}
{% endif %}
{% endspaceless %}
{% endblock form_widget_custom %}
现在渲染您的字段
{{ form_widget_custom(form.icon) }}
这篇关于根据项目覆盖表单呈现模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!