Skip to content

Commit

Permalink
Add support for css_classes in FormCheckBox
Browse files Browse the repository at this point in the history
Had to go the `template` field route so the
css_classes value can be accessed in the template properly
  • Loading branch information
stveit committed Nov 14, 2024
1 parent 9db1bbf commit 46fd948
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
5 changes: 3 additions & 2 deletions python/nav/web/crispyforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ class FormCheckBox:
:param field: A field to render as a checkbox field.
"""

def __init__(self, field):
def __init__(self, field, css_classes: Optional[str] = None):
self.field = field
self.input_type = 'checkbox'
self.css_classes = css_classes
self.template = 'custom_crispy_templates/form_checkbox.html'


class HelpField(Field):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
{% include 'custom_crispy_templates/submit_field.html' with input=field %}
{% elif field.input_type == 'helpfield' %}
{% include 'custom_crispy_templates/field_helptext_as_icon.html' with field=field.field %}
{% elif field.input_type == 'checkbox' %}
{% include 'custom_crispy_templates/form_checkbox.html' with field=field.field %}
{% else %}
{% show_field field %}
{% endif %}
24 changes: 13 additions & 11 deletions python/nav/web/templates/custom_crispy_templates/form_checkbox.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<div id="div_{{ field.auto_id }}" class="ctrlHolder{% if field.errors %} error{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}">
<label for="{{ field.id_for_label }}">
{{ field.label}}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
{{ field }}
</label>
{% for error in field.errors %}
<small id="error_{{ forloop.counter }}_{{ field.auto_id }}" class="errorField">
{{ error }}
</small>
{% endfor %}
</div>
{% with input=field.field %}
<div id="div_{{ input.auto_id }}" class="ctrlHolder{% if input.errors %} error{% endif %}{% if field.css_classes %} {{ field.css_classes }}{% endif %}">
<label for="{{ input.id_for_label }}">
{{ input.label}}{% if input.field.required %}<span class="asteriskField">*</span>{% endif %}
{{ input }}
</label>
{% for error in input.errors %}
<small id="error_{{ forloop.counter }}_{{ input.auto_id }}" class="errorField">
{{ error }}
</small>
{% endfor %}
</div>
{% endwith %}

0 comments on commit 46fd948

Please sign in to comment.