How to get input form type

前端 未结 5 1290
别跟我提以往
别跟我提以往 2021-01-03 23:46

I want get form field type and set class fot field type

i try:

{# Form field row #}
{% block form_row %}
{% spaceless %}
  
5条回答
  •  被撕碎了的回忆
    2021-01-04 00:12

    You can get the field type by using this:

    {{ form.FIELD_NAME.vars.block_prefixes.2 }}
    

    So if you got a field called message in your form use this:

    {{ form.message.vars.block_prefixes.2 }}
    

    For nested form field types use this:

    {{ form.NESTED_FORM_NAME.FIELD_NAME.vars.block_prefixes.2 }}
    

    EDIT :

    To overwrite the basic form blocks do this in your template file:

    ....
    {% 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 %}
    

    OR to get beter types:

    ....
    {% 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 %}
    

提交回复
热议问题