8000 [TwigBridge] Add support for toggle buttons in Bootstrap 5 form theme · symfony/symfony@5bcb636 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bcb636

Browse files
ker0xnicolas-grekas
authored andcommitted
[TwigBridge] Add support for toggle buttons in Bootstrap 5 form theme
1 parent 5bf31c5 commit 5bcb636

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `form_label_content` and `form_help_content` block to form themes
88
* Add `#[Template()]` to describe how to render arrays returned by controllers
9+
* Add support for toggle buttons in Bootstrap 5 form theme
910

1011
6.1
1112
---

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,30 +209,48 @@
209209
{%- endblock submit_widget %}
210210

211211
{%- block checkbox_widget -%}
212-
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-check-input')|trim}) -%}
212+
{%- set attr_class = attr_class|default(attr.class|default('')) -%}
213+
{%- set row_class = '' -%}
214+
{%- if 'btn-check' not in attr_class -%}
215+
{%- set attr_class = attr_class ~ ' form-check-input' -%}
216+
{%- set row_class = 'form-check' -%}
217+
{%- endif -%}
218+
{%- set attr = attr|merge({class: attr_class|trim}) -%}
213219
{%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
214-
{%- set row_class = 'form-check' -%}
215220
{%- if 'checkbox-inline' in parent_label_class %}
216221
{% set row_class = row_class ~ ' form-check-inline' %}
217222
{% endif -%}
218223
{%- if 'checkbox-switch' in parent_label_class %}
219224
{% set row_class = row_class ~ ' form-switch' %}
220225
{% endif -%}
221-
<div class="{{ row_class }}">
222-
{{- form_label(form, null, { widget: parent() }) -}}
223-
</div>
226+
{%- if row_class is not empty -%}
227+
<div class="{{ row_class }}">
228+
{%- endif -%}
229+
{{- form_label(form, null, { widget: parent() }) -}}
230+
{%- if row_class is not empty -%}
231+
</div>
232+
{%- endif -%}
224233
{%- endblock checkbox_widget %}
225234

226235
{%- block radio_widget -%}
227-
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-check-input')|trim}) -%}
236+
{%- set attr_class = attr_class|default(attr.class|default('')) -%}
237+
{%- set row_class = '' -%}
238+
{%- if 'btn-check' not in attr_class -%}
239+
{%- set attr_class = attr_class ~ ' form-check-input' -%}
240+
{%- set row_class = 'form-check' -%}
241+
{%- endif -%}
242+
{%- set attr = attr|merge({class: attr_class|trim}) -%}
228243
{%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
229-
{%- set row_class = 'form-check' -%}
230244
{%- if 'radio-inline' in parent_label_class -%}
231245
{%- set row_class = row_class ~ ' form-check-inline' -%}
232246
{%- endif -%}
233-
<div class="{{ row_class }}">
234-
{{- form_label(form, null, { widget: parent() }) -}}
235-
</div>
247+
{%- if row_class is not empty -%}
248+
<div class="{{ row_class }}">
249+
{%- endif -%}
250+
{{- form_label(form, null, { widget: parent() }) -}}
251+
{%- if row_class is not empty -%}
252+
</div>
253+
{%- endif -%}
236254
{%- endblock radio_widget %}
237255

238256
{%- block choice_widget_collapsed -%}
@@ -276,7 +294,11 @@
276294
{%- block checkbox_radio_label -%}
277295
{#- Do not display the label if widget is not defined in order to prevent double label rendering -#}
278296
{%- if widget is defined -%}
279-
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' form-check-label')|trim}) -%}
297+
{%- set label_attr_class = label_attr_class|default(label_attr.class|default('')) -%}
298+
{%- if 'btn' not in label_attr_class -%}
299+
{%- set label_attr_class = label_attr_class ~ ' form-check-label' -%}
300+
{%- endif -%}
301+
{%- set label_attr = label_attr|merge({class: label_attr_class|trim}) -%}
280302
{%- if not compound -%}
281303
{% set label_attr = label_attr|merge({'for': id}) %}
282304
{%- endif -%}

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap5LayoutTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,21 @@ public function testCheckboxSwitchWithValue()
404404
);
405405
}
406406

407+
public function testCheckboxToggleWithValue()
408+
{
409+
$form = $this->factory->createNamed('name', CheckboxType::class, false, [
410+
'value' => 'foo&bar',
411+
]);
412+
413+
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'btn-check my&class'], 'label_attr' => ['class' => 'btn btn-primary']],
414+
'/input[@type="checkbox"][@name="name"][@id="my&id"][@class="btn-check my&class"][@value="foo&bar"]
415+
/following-sibling::label
416+
[@class="btn btn-primary required"]
417+
[.="[trans]Name[/trans]"]
418+
'
419+
);
420+
}
421+
407422
public function testMultipleChoiceSkipsPlaceholder()
408423
{
409424
$form = $this->factory->createNamed('name', ChoiceType::class, ['&a'], [

0 commit comments

Comments
 (0)
0