abernard
abernard

Reputation: 231

form_label() not working with boolean type

I have a form that I render in a twig file :

form :

$builder

        ->add('text', CKEditorType::class, array (
            'config_name' => 'my_config',
            'label' => "tracker.event.labels.text"

        ))

        ->add('effTime', IntegerType::class, array (
            'data' =>  0,
            'label' => "tracker.event.labels.efftime"
        )) 

        ->add('private', CheckboxType::class, array(
            'label' => "tracker.event.private.title",
            'data' => false,
            'required' => false
        ))
    ;

twig file :

{% form_theme formadd 'AtgpAppBundle:_Form:_form.html.twig' %}

                    {{ form_start(formadd) }}
                    {{ form_errors(formadd) }}
                    <div class ="row">
                        <span class="col-md-2">
                            {{form_label(formadd.text)}}
                        </span>
                        <span class="col-md-10">
                            {{form_errors(formadd.text)}}
                            {{form_widget(formadd.text)}}
                        </span>
                    </div>
                    <div class="row">
                        <span class="col-md-2">
                            {{form_label(formadd.effTime)}}
                        </span>
                        <span class="col-md-10">
                            {{form_errors(formadd.effTime)}}
                            {{form_widget(formadd.effTime)}}
                        </span>
                    </div>
                    <div class="row">
                        <span class="col-md-2"> 
                            {{form_label(formadd.private)}}
                        </span>
                        <span class="col-md-1">
                            {{form_errors(formadd.private)}}
                            {{form_widget(formadd.private) }}
                        </span>
                        <span class="col-md-9">
                            {{ "tracker.event.private.help" |trans }}
                        </span>
                    </div>
                    {{ form_end(formadd) }}

Everything works nicely, except for the last row.

The col-md-2, where the label for "private" should be, is empty.

For some reason form_widget created an other

<div class="row"> 

inside of the span "class=col-md-1"

with the label and the checkbox inside.

Here's how the whole row renders

I would like the label properly displayed in the right place. The instruction form_label() is simply ignored, if I don't write it it renders the same.

How can I do that, and what is wrong with my code ?

Thank you

EDIT : I figured out that I can just set the label value to false and write it directly in twig. However I still have no clue why the instruction form_label is ignored with booleans.

Upvotes: 1

Views: 562

Answers (2)

abernard
abernard

Reputation: 231

I found the answer to my question in the documentation.

We can read this :

When you use the Bootstrap form themes and render the fields manually, calling form_label() for a checkbox/radio field doesn't show anything. Due to Bootstrap internals, the label is already shown by form_widget().

So what I needed to do, was to override the checkbox-widget block from this :

{% block checkbox_widget -%}
    {%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
    {% if 'checkbox-inline' in parent_label_class %}
        {{- form_label(form, null, { widget: parent() }) -}}
    {% else -%}
        <div class="checkbox">
            {{- form_label(form, null, { widget: parent() }) -}}
        </div>
    {%- endif %}
{%- endblock checkbox_widget %}

To this :

{% block checkbox_widget %}
        <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
{% endblock %}

Upvotes: 1

Sylvain Guilbert
Sylvain Guilbert

Reputation: 722

wild guess : all your labels are like tracker.event.labels.XXXXX where as the last one is tracker.event.private.title shouldn't it be tracker.event.labels.private.title

Upvotes: 0

Related Questions