From 83afb9be37f5e13055d56729242e9c3c79111e59 Mon Sep 17 00:00:00 2001 From: powderflask Date: Sat, 6 Jan 2024 19:15:17 -0800 Subject: [PATCH] fix for issue #5 handle case where checkbox is rendered without label. regression test added: test fails in 2022.1 passes with this fix. --- crispy_bootstrap3/templates/bootstrap3/field.html | 12 ++++++++---- tests/forms.py | 6 ++++++ tests/test_layout.py | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/crispy_bootstrap3/templates/bootstrap3/field.html b/crispy_bootstrap3/templates/bootstrap3/field.html index 3d8d543..8e80a90 100644 --- a/crispy_bootstrap3/templates/bootstrap3/field.html +++ b/crispy_bootstrap3/templates/bootstrap3/field.html @@ -25,11 +25,15 @@ {% endif %} {% if not field|is_checkboxselectmultiple and not field|is_radioselect %} - {% if field|is_checkbox and form_show_labels %} - + {% endif %} {% include 'bootstrap3/layout/help_text_and_errors.html' %} {% else %}
diff --git a/tests/forms.py b/tests/forms.py index 6fd9459..0f7ee1e 100644 --- a/tests/forms.py +++ b/tests/forms.py @@ -82,6 +82,12 @@ class CheckboxesSampleForm(forms.Form): ) +class SimpleCheckboxSampleForm(forms.Form): + is_company = forms.CharField( + label="company", required=False, widget=forms.CheckboxInput() + ) + + class SelectSampleForm(forms.Form): select = forms.ChoiceField( choices=((1, "Option one"), (2, "Option two"), (3, "Option three")), diff --git a/tests/test_layout.py b/tests/test_layout.py index 8e5855c..8f1649a 100644 --- a/tests/test_layout.py +++ b/tests/test_layout.py @@ -35,6 +35,7 @@ SampleForm4, SampleForm5, SampleForm6, + SimpleCheckboxSampleForm, ) from .test_settings import TEMPLATE_DIRS from .utils import contains_partial, parse_expected, parse_form @@ -586,6 +587,19 @@ def test_multiple_checkboxes_bs3(): ) +def test_no_label_checkboxes_bs3(): + form = SimpleCheckboxSampleForm() + form.helper = FormHelper() + # no form-control class when labels are rendered + html = render_crispy_form(form) + assert html.count("form-control") == 0 + form.helper.form_show_labels = False + # no labels or form-control class when labels are hidden + html = render_crispy_form(form) + assert html.count("