Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get input form type

Tags:

twig

symfony

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

i try:

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

But {{ type }} not working.

like image 345
ZhukV Avatar asked Nov 02 '12 13:11

ZhukV


3 Answers

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.

like image 154
A.L Avatar answered Sep 21 '22 19:09

A.L


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 %}
like image 45
Mats Rietdijk Avatar answered Sep 21 '22 19:09

Mats Rietdijk


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.

like image 27
dryobs Avatar answered Sep 22 '22 19:09

dryobs