Created
March 2, 2016 09:03
-
-
Save webdevilopers/97d400ddae5960a9f84d to your computer and use it in GitHub Desktop.
Override label class in Bootstrap 3 horizontal layout in Symfony Twig Bridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% use "bootstrap_3_layout.html.twig" %} | |
{% block form_start -%} | |
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-horizontal')|trim}) %} | |
{{- parent() -}} | |
{%- endblock form_start %} | |
{# Labels #} | |
{% block form_label -%} | |
{% spaceless %} | |
{% if label is same as(false) %} | |
<div class="{{ block('form_label_class') }}"></div> | |
{% else %} | |
{% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ block('form_label_class'))|trim}) %} | |
{{- parent() -}} | |
{% endif %} | |
{% endspaceless %} | |
{%- endblock form_label %} | |
{% block form_label_class -%} | |
col-sm-2 | |
{%- endblock form_label_class %} | |
{# Rows #} | |
{% block form_row -%} | |
<div class="form-group{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}"> | |
{{- form_label(form) -}} | |
<div class="{{ block('form_group_class') }}"> | |
{{- form_widget(form) -}} | |
{{- form_errors(form) -}} | |
</div> | |
{##}</div> | |
{%- endblock form_row %} | |
{% block checkbox_row -%} | |
{{- block('checkbox_radio_row') -}} | |
{%- endblock checkbox_row %} | |
{% block radio_row -%} | |
{{- block('checkbox_radio_row') -}} | |
{%- endblock radio_row %} | |
{% block checkbox_radio_row -%} | |
{% spaceless %} | |
<div class="form-group{% if not valid %} has-error{% endif %}"> | |
<div class="{{ block('form_label_class') }}"></div> | |
<div class="{{ block('form_group_class') }}"> | |
{{ form_widget(form) }} | |
{{ form_errors(form) }} | |
</div> | |
</div> | |
{% endspaceless %} | |
{%- endblock checkbox_radio_row %} | |
{% block submit_row -%} | |
{% spaceless %} | |
<div class="form-group"> | |
<div class="{{ block('form_label_class') }}"></div> | |
<div class="{{ block('form_group_class') }}"> | |
{{ form_widget(form) }} | |
</div> | |
</div> | |
{% endspaceless %} | |
{% endblock submit_row %} | |
{% block reset_row -%} | |
{% spaceless %} | |
<div class="form-group"> | |
<div class="{{ block('form_label_class') }}"></div> | |
<div class="{{ block('form_group_class') }}"> | |
{{ form_widget(form) }} | |
</div> | |
</div> | |
{% endspaceless %} | |
{% endblock reset_row %} | |
{% block form_group_class -%} | |
col-sm-10 | |
{%- endblock form_group_class %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% block form_label_class -%} | |
col-sm-4 | |
{%- endblock form_label_class %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'SonataAdminBundle::standard_layout.html.twig' %} | |
{% block content %} | |
{% form_theme form 'bootstrap_3_horizontal_layout.html.twig' %} | |
{{ form_start(form, {'attr': {'novalidate': 'novalidate'} } ) }} | |
{{ form_widget(form.foo) }} | |
{{ form_row(form._token) }} | |
{{ form_end(form, {'render_rest': false}) }} | |
{% endblock %} |
Any idea @MatTheCat @lyrixx @javiereguiluz?
I solved this by creating my own form theme and extending the bootstrap_3_horizontal_layout theme:
{# app/Resources/views/Form/updated_bootstrap_3_horizontal_layout.html.twig #}
{% use "bootstrap_3_horizontal_layout.html.twig" %}
{% block form_label_class %}
col-sm-4
{% endblock %}
{% block form_group_class %}
col-sm-8
{% endblock %}
Then in the output file, use your new custom theme:
{% form_theme myForm 'Form/bootstrap_3_updated_horizontal_layout.html.twig' %}
Use this param horizontal_label_class
to set class or false in form elements.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This way the
label_class
will not change. It still usescol-sm-2
instead ofcol-sm-2
defined here:https://github.com/symfony/twig-bridge/blob/master/Resources/views/Form/bootstrap_3_horizontal_layout.html.twig#L21-L23
Any idea?
Original discussion:
https://twitter.com/webdevilopers/status/704774817963237376