您可以使用以下方法获取字段类型:
{{ form.FIELD_NAME.vars.block_prefixes.2 }}
因此,如果您的表单中有一个名为 message 的字段,请使用:
{{ form.message.vars.block_prefixes.2 }}
对于嵌套表单字段类型,请使用:
{{ form.NESTED_FORM_NAME.FIELD_NAME.vars.block_prefixes.2 }}
编辑 :
要覆盖基本表单块,请在模板文件中执行以下操作:
....
{% form_theme form _self %}
{% block widget_attributes %}
{% spaceless %}
id="{{ id }}" name="{{ full_name }}"{% if read_only %} readonly="readonly"{% endif %}{% if disabled %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
{% if not attr.class is defined %}
class="{{ type|default('text') }}"
{% endif %}
{% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% elseif attrname == 'class' %}{{ attrname }}="{{ type|default('text') }} {{ attrvalue }}"{% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}
{% block content %}
....
{% endblock %}
或获得更好的类型:
....
{% form_theme form _self %}
{% block widget_attributes %}
{% spaceless %}
id="{{ id }}" name="{{ full_name }}"{% if read_only %} readonly="readonly"{% endif %}{% if disabled %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %}
{% if not attr.class is defined %}
class="{{ form.vars.block_prefixes.2 }}"
{% endif %}
{% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% elseif attrname == 'class' %}{{ attrname }}="{{ form.vars.block_prefixes.2 }} {{ attrvalue }}"{% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{% endspaceless %}
{% endblock widget_attributes %}
{% block content %}
....
{% endblock %}