Skip to content

Commit ef66ad3

Browse files
committed
feature #33954 Form theme: support Bootstrap 4 custom switches (romaricdrigon)
This PR was merged into the 4.4 branch. Discussion ---------- Form theme: support Bootstrap 4 custom switches | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | / | License | MIT | Doc PR | symfony/symfony-docs#12464 Hello, At the moment, Symfony form theme supports [custom checkboxes](https://getbootstrap.com/docs/4.3/components/forms/#checkboxes) through an extra class in `label_attr`. Bootstrap4 introduced also [custom switches](https://getbootstrap.com/docs/4.3/components/forms/#switches), which has exactly the same HTML markup, but use a different class. This PR slightly modify `bootstrap_4_layout` to handle it. ![image](https://user-images.githubusercontent.com/919405/66651725-0eaa3100-ec34-11e9-8b68-94324730ac80.png) Some reasons why I think supporting those have its place in Symfony: - those are getting common in UI right now, it is a common use case - it is complementary to normal checkboxes, and works the same way: required attribute, validation error, and so on are supported immediately - implementing it yourself in your form theme is actually tricky, because of the way checkbox are handled (ie., `form_label` called inside `form_widget` with a `{ widget: parent() }`). You have to overwrite the whole fragment, otherwise you get an infinite recursion. Finally, some screenshots and code examples. Custom checkbox (as at the moment): ![image](https://user-images.githubusercontent.com/919405/66652982-41a1f400-ec37-11e9-813f-4b39087e89e7.png) ```php ->add('test', CheckboxType::class, [ 'label_attr' => [ 'class' => 'checkbox-custom', ], ]) ``` Custom switch (proposed): ![image](https://user-images.githubusercontent.com/919405/66652902-1919fa00-ec37-11e9-98f3-9340b01b2335.png) ```php ->add('test', CheckboxType::class, [ 'label_attr' => [ 'class' => 'switch-custom', ], ]) ``` Commits ------- 99f59e2 Supporting Bootstrap 4 custom switches
2 parents 25baf75 + 99f59e2 commit ef66ad3

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* the `LintCommand` lints all the templates stored in all configured Twig paths if none argument is provided
1111
* deprecated accepting STDIN implicitly when using the `lint:twig` command, use `lint:twig -` (append a dash) instead to make it explicit.
1212
* added `--show-deprecations` option to the `lint:twig` command
13+
* added support for Bootstrap4 switches, use `switch-custom` as `label_attr` in a `CheckboxType`
1314

1415
4.3.0
1516
-----

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@
166166
<div class="custom-control custom-checkbox{{ 'checkbox-inline' in parent_label_class ? ' custom-control-inline' }}">
167167
{{- form_label(form, null, { widget: parent() }) -}}
168168
</div>
169+
{%- elseif 'switch-custom' in parent_label_class -%}
170+
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' custom-control-input')|trim}) -%}
171+
<div class="custom-control custom-switch{{ 'switch-inline' in parent_label_class ? ' custom-control-inline' }}">
172+
{{- form_label(form, null, { widget: parent() }) -}}
173+
</div>
169174
{%- else -%}
170175
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-check-input')|trim}) -%}
171176
<div class="form-check{{ 'checkbox-inline' in parent_label_class ? ' form-check-inline' }}">
@@ -238,7 +243,7 @@
238243
{#- Do not display the label if widget is not defined in order to prevent double label rendering -#}
239244
{%- if widget is defined -%}
240245
{% set is_parent_custom = parent_label_class is defined and ('checkbox-custom' in parent_label_class or 'radio-custom' in parent_label_class) %}
241-
{% set is_custom = label_attr.class is defined and ('checkbox-custom' in label_attr.class or 'radio-custom' in label_attr.class) %}
246+
{% set is_custom = label_attr.class is defined and ('checkbox-custom' in label_attr.class or 'radio-custom' in label_attr.class or 'switch-custom' in label_attr.class) %}
242247
{%- if is_parent_custom or is_custom -%}
243248
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' custom-control-label')|trim}) -%}
244249
{%- else %}

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_base_layout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172

173173
{% block choice_label -%}
174174
{# remove the checkbox-inline and radio-inline class, it's only useful for embed labels #}
175-
{%- set label_attr = label_attr|merge({class: label_attr.class|default('')|replace({'checkbox-inline': '', 'radio-inline': '', 'checkbox-custom': '', 'radio-custom': ''})|trim}) -%}
175+
{%- set label_attr = label_attr|merge({class: label_attr.class|default('')|replace({'checkbox-inline': '', 'radio-inline': '', 'checkbox-custom': '', 'radio-custom': '', 'switch-custom': ''})|trim}) -%}
176176
{{- block('form_label') -}}
177177
{% endblock choice_label %}
178178

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy