Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a checkbox in Symfony's Twig Form template

I am currently trying to override the default rendering of checkbox blocks in Symfony 2, but I can't achieve the desired result.

I have created an Doctrine Entity called "Categories", and all the views were properly created.

But the default checkboxes labels in Twig form are not in the correct positioning.

PS.: I removed the attributes from the elements here to make it more clean to read.

As is:

<label>Field</label>
<input type="checkbox" />

Should be:

<label><input type="checkbox" />Field</label>

I've created a template for overriding the block itself:

{% block checkbox_widget %}
{% spaceless %}
<label for="{{ id }}">
<input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />
{{ label|trans({}, translation_domain) }}</label>
{% endspaceless %}
{% endblock checkbox_widget %}

And called it in my edit.html.twig file:

{% extends '::base.html.twig' %}
{% form_theme edit_form 'AppGallerySiteBundle:Form:fields.html.twig' %}

Inside the form I placed the lines:

{{ form_errors(edit_form) }}
{{ form_row(edit_form) }}
{{ form_widget(edit_form) }}
{{ form_rest(edit_form) }}

This works and the checkboxes are overrided, BUT the ordinary label from default twig form template continues being displayed and my overrided checkbox doesn't show the label inside it resulting in:

<label>Field</label>
<label><input type="checkbox" /></label>

Where it should be:

<label><input type="checkbox" />Field</label>

Hope somebody could help. Thanks in advance.

like image 799
Gilberto Albino Avatar asked Aug 09 '13 01:08

Gilberto Albino


People also ask

Can I override the label in Symfony templates?

The templates provided in Symfony only allow to override the label as shown in the example above. See " More about Form Variables " to learn about the variables argument.

What is form_row () in Symfony?

Renders the "row" of a given field, which is the combination of the field's label, errors and widget. The second argument to form_row () is an array of variables. The templates provided in Symfony only allow to override the label as shown in the example above. See " More about Form Variables " to learn about the variables argument.

How to validate whether the city and ZIP code match in Symfony?

Imagine you have a custom method named matchingCityAndZipCode () that validates whether the city and zip code match. Unfortunately, there is no matchingCityAndZipCode field in your form, so all that Symfony can do is display the error on top of the form.

Should I let Symfony guess my field type?

At best, if you let Symfony guess your field type, then the value of this option will be guessed from your validation information. The required option also affects how empty data for each field is handled.


2 Answers

You should disable rendering label for checkboxes. So you need to override block form_label like this:

{% block form_label %}
{% if 'checkbox' not in block_prefixes %}
  {{ parent() }}
{% endif %}
{% endblock form_label %}
like image 123
Przemysław Piechota. kibao Avatar answered Sep 29 '22 10:09

Przemysław Piechota. kibao


In order to correct the order of input and label, you can override the form_row block:

{% block form_row %}
  {% spaceless %}
    {% if 'checkbox' in block_prefixes %}
      {{ form_widget(form) }}
      {{ form_label(form) }}
    {% else %}
      {{ form_label(form) }}
      {{ form_widget(form) }}
    {% endif %}
    {{ form_errors(form) }}
  {% endspaceless %}
{% endblock form_row %}
like image 34
joshudev Avatar answered Sep 29 '22 12:09

joshudev