Skip to content

Commit

Permalink
Fixed form-control classes with checkboxes (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
powderflask authored Jan 9, 2024
1 parent ffa7878 commit 1dbae65
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
12 changes: 8 additions & 4 deletions crispy_bootstrap3/templates/bootstrap3/field.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@
{% endif %}

{% if not field|is_checkboxselectmultiple and not field|is_radioselect %}
{% if field|is_checkbox and form_show_labels %}
<label for="{{ field.id_for_label }}" class="{% if field.field.required %} requiredField{% endif %}">
{% if field|is_checkbox %}
{% if form_show_labels %}
<label for="{{ field.id_for_label }}" class="{% if field.field.required %} requiredField{% endif %}">
{% crispy_field field %}
{{ field.label }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
</label>
{% else %}
{% crispy_field field %}
{{ field.label }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
</label>
{% endif %}
{% include 'bootstrap3/layout/help_text_and_errors.html' %}
{% else %}
<div class="controls {{ field_class }}">
Expand Down
6 changes: 6 additions & 0 deletions tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
14 changes: 14 additions & 0 deletions tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
SampleForm4,
SampleForm5,
SampleForm6,
SimpleCheckboxSampleForm,
)
from .test_settings import TEMPLATE_DIRS
from .utils import contains_partial, parse_expected, parse_form
Expand Down Expand Up @@ -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("<label ") == 0
assert html.count("form-control") == 0


@pytest.mark.skipif(__version__[0] == "1", reason="#1262 fixed required attributes.")
def test_radio_bs3():
form = SampleForm5()
Expand Down

0 comments on commit 1dbae65

Please sign in to comment.