Skip to content

Commit 1dbae65

Browse files
authored
Fixed form-control classes with checkboxes (#6)
1 parent ffa7878 commit 1dbae65

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

crispy_bootstrap3/templates/bootstrap3/field.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
{% endif %}
2626

2727
{% if not field|is_checkboxselectmultiple and not field|is_radioselect %}
28-
{% if field|is_checkbox and form_show_labels %}
29-
<label for="{{ field.id_for_label }}" class="{% if field.field.required %} requiredField{% endif %}">
28+
{% if field|is_checkbox %}
29+
{% if form_show_labels %}
30+
<label for="{{ field.id_for_label }}" class="{% if field.field.required %} requiredField{% endif %}">
31+
{% crispy_field field %}
32+
{{ field.label }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
33+
</label>
34+
{% else %}
3035
{% crispy_field field %}
31-
{{ field.label }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
32-
</label>
36+
{% endif %}
3337
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
3438
{% else %}
3539
<div class="controls {{ field_class }}">

tests/forms.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ class CheckboxesSampleForm(forms.Form):
8282
)
8383

8484

85+
class SimpleCheckboxSampleForm(forms.Form):
86+
is_company = forms.CharField(
87+
label="company", required=False, widget=forms.CheckboxInput()
88+
)
89+
90+
8591
class SelectSampleForm(forms.Form):
8692
select = forms.ChoiceField(
8793
choices=((1, "Option one"), (2, "Option two"), (3, "Option three")),

tests/test_layout.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
SampleForm4,
3636
SampleForm5,
3737
SampleForm6,
38+
SimpleCheckboxSampleForm,
3839
)
3940
from .test_settings import TEMPLATE_DIRS
4041
from .utils import contains_partial, parse_expected, parse_form
@@ -586,6 +587,19 @@ def test_multiple_checkboxes_bs3():
586587
)
587588

588589

590+
def test_no_label_checkboxes_bs3():
591+
form = SimpleCheckboxSampleForm()
592+
form.helper = FormHelper()
593+
# no form-control class when labels are rendered
594+
html = render_crispy_form(form)
595+
assert html.count("form-control") == 0
596+
form.helper.form_show_labels = False
597+
# no labels or form-control class when labels are hidden
598+
html = render_crispy_form(form)
599+
assert html.count("<label ") == 0
600+
assert html.count("form-control") == 0
601+
602+
589603
@pytest.mark.skipif(__version__[0] == "1", reason="#1262 fixed required attributes.")
590604
def test_radio_bs3():
591605
form = SampleForm5()

0 commit comments

Comments
 (0)