我想要获取表单字段类型并设置类fot字段类型
我试试:
{# Form field row #} {% block form_row %} {% spaceless %} <div class="field-group{% if errors|length > 0%} error{%endif%}" id="fc-{{ id }}"> {{ form_label(form,label|default(null)) }} <div class="field-item {{ type }}"> {{ form_widget(form) }} {% if errors|length > 0 %} <div class="errors">{{ form_errors(form) }}</div> {% endif %} </div> </div> {% endspaceless %} {% endblock %}
但{{type}}不工作.
解决方法
您可以通过以下方式获取字段类型:
{{ form.FIELD_NAME.vars.block_prefixes.2 }}
所以如果你有一个字段叫做消息在你的表单使用这个:
{{ 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,translation_domain) }}" {% elseif attrname == 'class' %}{{ attrname }}="{{ form.vars.block_prefixes.2 }} {{ attrvalue }}"{% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %} {% endspaceless %} {% endblock widget_attributes %} {% block content %} .... {% endblock %}