How to get input form type

前端 未结 5 1293
别跟我提以往
别跟我提以往 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: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 %}
        
    {{ form_label(form) }} {{ form_widget(form) }} {{ form_errors(form) }}
    {% 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 %}
        
    {{ form_label(form) }} {{ form_widget(form) }} {{ form_errors(form) }}
    {% 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.

提交回复
热议问题