Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig Form Theming - form_label class attribute

Tags:

twig

symfony

I've been trying to override the form_row Twig extension so I can render a Twitter Bootstrap style row. A twitter boostrap form row should look like this:

<div class="control-group">
    <label class="control-label" for="idOfMainInput">Label Name:</label>
    <div class="controls">
        <input type="text" id="idOfMainInput" name="someDumbName">
    </div>
</div>

The base twig div style form_row block is defined in this link as below:

{% block form_row %}
{% spaceless %}
    <div>
        {{ form_label(form) }}
        {{ form_errors(form) }}
        {{ form_widget(form) }}
    </div>
{% endspaceless %}
{% endblock form_row %}

So, my thoughts were to just put the necessary divs in, and hard code where necessary the class entries (i.e. in the main div) but pass the 'attr' value to the form_label, form_errors and form_widget sections. I've taken out form_errors for now, just so I don't get too deep into it. Here's what I tried:

{% form_theme form _self %}
{% block form_row %}
{% spaceless %}
<div class="control-group">
    {{ form_label(form, 'test label name', { 'attr': {'class': 'control-label'} }) }}
    <div class="controls">
    {{ form_widget(form) }}
    {{ form_errors(form) }}
    </div>
</div>
{% endspaceless %}
{% endblock form_row %}

The problem, though, is no matter what I try, the form_label extension does not use "control-label" as my class (and it should according to the source code append it if there's existing ones, like "required"). Here's what I get when I view the source of the rendered page:

<div class="control-group">
    <label for="form_rsa_id" class="required">test label name</label>
    <div class="controls">
        <input type="number" id="form_rsa_id" name="form[rsa_id]" required="required" />
    </div>
</div>

As you can see, the class="required" is there and is taken from the base form object attributes, but it should be class="required control-label", which it's not.

Kinda at a loss here, as the documentation (as well as the source) states that one should use the notation "form_label(view, label, variables)". Link to docs here.

like image 351
Sarel Avatar asked Apr 01 '13 13:04

Sarel


1 Answers

I think you need to use label_attr rather than attr.

like image 200
qooplmao Avatar answered Oct 08 '22 17:10

qooplmao