How to get input form type

前端 未结 5 1291
别跟我提以往
别跟我提以往 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 %}
    
    0 讨论(0)
  • 2021-01-04 00:21

    To avoid hardcoding the array index (1 or 2 depending on Symfony version), i've used the following:

    {{ form.vars.block_prefixes | slice(-2,1) | first }}
    
    0 讨论(0)
  • 2021-01-04 00:31

    The answer from MatsRietdijk is right, but as of Symfony 2.3 the index of the type seems to have changed from 2 to 1. As a result, {{ form.vars.block_prefixes.1 }} will return checkbox, date, choice, etc.

    You can use it to add a class to the form row when making application-wide customizations:

    {% block form_row %}
        <div class="form_row {{ form.vars.block_prefixes.1 }}">
            {{ form_label(form) }}
            {{ form_widget(form) }}
            {{ form_errors(form) }}
        </div>
    {% endblock form_row %}
    

    Then you can apply CSS rules:

    div.form_row.text {color:Red;}
    

    Twitter Bootstrap

    If you use Twitter bootstrap, you may have problems since the .checkbox class exists in bootstrap. I suggest to use a prefix for Symfony forms rows:

    {% block form_row %}
        <div class="form_row symfony_{{ form.vars.block_prefixes.1 }}">
            {{ form_label(form) }}
            {{ form_widget(form) }}
            {{ form_errors(form) }}
        </div>
    {% endblock form_row %}
    

    The rules in the CSS files will be different:

    div.form_row.symfony_text {color:Red;}
    

    Update

    Twitter bootstrap form theme is now included in Symfony 2.6.

    0 讨论(0)
  • 2021-01-04 00:35

    Use

    {{ form.name.vars.original_type }}
    

    E.g. from one of my templates

    {% if myform.fieldname.vars.original_type == 'choice' %}
        <hr>
        {{ form_label(myform.fieldname) }}
        <p>Select a ...</p>
        {{ form_widget(myform.fieldname) }}
    {% elseif myform.fieldname.vars.original_type == 'hidden' %}
        {{ form_row(myform.fieldname) }}
    {% endif %}
    
    0 讨论(0)
  • 2021-01-04 00:38

    block_prefixes is used to generate block names to display field (cf FormRenderer::searchAndRenderBlock method). The most specific existing block in template (generally form_div_layout.html.twig file) will be displayed.

    So, the last item of block_prefixes is the id of your input, to allow you to override block for a specific field.

    Previous item will be the item you need.

    You can use this one : $form->vars.block_prefixes[$form->vars.block_prefixes|count -2] in smarty syntax.

    0 讨论(0)
提交回复
热议问题