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.
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.
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.
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.
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.
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 %}
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With