8000 [TwigBridge] Add support for toggle buttons in Bootstrap 5 form theme by ker0x · Pull Request #43865 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge] Add support for toggle buttons in Bootstrap 5 form theme #43865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

6.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,30 +209,48 @@
{%- endblock submit_widget %}

{%- block checkbox_widget -%}
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-check-input')|trim}) -%}
{%- set attr_class = attr_class|default(attr.class|default('')) -%}
{%- set row_class = '' -%}
{%- if 'btn-check' not in attr_class -%}
{%- set attr_class = attr_class ~ ' form-check-input' -%}
{%- set row_class = 'form-check' -%}
{%- endif -%}
{%- set attr = attr|merge({class: attr_class|trim}) -%}
{%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
{%- set row_class = 'form-check' -%}
{%- if 'checkbox-inline' in parent_label_class %}
{% set row_class = row_class ~ ' form-check-inline' %}
{% endif -%}
{%- if 'checkbox-switch' in parent_label_class %}
{% set row_class = row_class ~ ' form-switch' %}
{% endif -%}
<div class="{{ row_class }}">
{{- form_label(form, null, { widget: parent() }) -}}
</div>
{%- if row_class is not empty -%}
<div class="{{ row_class }}">
{%- endif -%}
{{- form_label(form, null, { widget: parent() }) -}}
{%- if row_class is not empty -%}
</div>
{%- endif -%}
{%- endblock checkbox_widget %}

{%- block radio_widget -%}
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-check-input')|trim}) -%}
{%- set attr_class = attr_class|default(attr.class|default('')) -%}
{%- set row_class = '' -%}
{%- if 'btn-check' not in attr_class -%}
{%- set attr_class = attr_class ~ ' form-check-input' -%}
{%- set row_class = 'form-check' -%}
{%- endif -%}
{%- set attr = attr|merge({class: attr_class|trim}) -%}
{%- set parent_label_class = parent_label_class|default(label_attr.class|default('')) -%}
{%- set row_class = 'form-check' -%}
{%- if 'radio-inline' in parent_label_class -%}
{%- set row_class = row_class ~ ' form-check-inline' -%}
{%- endif -%}
<div class="{{ row_class }}">
{{- form_label(form, null, { widget: parent() }) -}}
</div>
{%- if row_class is not empty -%}
<div class="{{ row_class }}">
{%- endif -%}
{{- form_label(form, null, { widget: parent() }) -}}
{%- if row_class is not empty -%}
</div>
{%- endif -%}
{%- endblock radio_widget %}

{%- block choice_widget_collapsed -%}
Expand Down Expand Up @@ -276,7 +294,11 @@
{%- block checkbox_radio_label -%}
{#- Do not display the label if widget is not defined in order to prevent double label rendering -#}
{%- if widget is defined -%}
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' form-check-label')|trim}) -%}
{%- set label_attr_class = label_attr_class|default(label_attr.class|default('')) -%}
{%- if 'btn' not in label_attr_class -%}
{%- set label_attr_class = label_attr_class ~ ' form-check-label' -%}
{%- endif -%}
{%- set label_attr = label_attr|merge({class: label_attr_class|trim}) -%}
{%- if not compound -%}
{% set label_attr = label_attr|merge({'for': id}) %}
{%- endif -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,21 @@ public function testCheckboxSwitchWithValue()
);
}

public function testCheckboxToggleWithValue()
{
$form = $this->factory->createNamed('name', CheckboxType::class, false, [
'value' => 'foo&bar',
]);

$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'btn-check my&class'], 'label_attr' => ['class' => 'btn btn-primary']],
'/input[@type="checkbox"][@name="name"][@id="my&id"][@class="btn-check my&class"][@value="foo&bar"]
/following-sibling::label
[@class="btn btn-primary required"]
[.="[trans]Name[/trans]"]
'
);
}

public function testMultipleChoiceSkipsPlaceholder()
{
$form = $this->factory->createNamed('name', ChoiceType::class, ['&a'], [
Expand Down
0