Skip to content

Commit 2b6c8fa

Browse files
committed
Allow per-form override of settings.TBXFORMS_HIGHLIGHT_REQUIRED_FIELDS
1 parent 768d7f0 commit 2b6c8fa

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

tbxforms/helper.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class FormHelper(crispy_forms_helper.FormHelper):
1616
adding the following attributes to control how the form is rendered.
1717
1818
Attributes:
19+
highlight_required_fields (:obj:`bool`, optional): whether to highlight
20+
required fields or optional fields. If not set on the form, the
21+
`TBXFORMS_HIGHLIGHT_REQUIRED_FIELDS` setting will be used, falling
22+
to `False` if that setting is not set.
23+
1924
label_size (:obj:`str`, optional): set the default size used for all
2025
field labels. The default value of None renders labels with the
2126
same font size as body text. To change the font size and weight
@@ -55,6 +60,7 @@ def __init__(self, *args, **kwargs):
5560
5661
"""
5762

63+
highlight_required_fields = None
5864
label_size = ""
5965
legend_size = ""
6066
show_error_summary = True
@@ -76,9 +82,14 @@ def render_layout(self, form, context, template_pack=TEMPLATE_PACK):
7682
if self.legend_size:
7783
context["legend_size"] = Size.for_legend(self.legend_size)
7884

79-
context["highlight_required_fields"] = getattr(
80-
settings, "TBXFORMS_HIGHLIGHT_REQUIRED_FIELDS", False
81-
)
85+
if self.highlight_required_fields is not None:
86+
context["highlight_required_fields"] = (
87+
self.highlight_required_fields
88+
)
89+
else:
90+
context["highlight_required_fields"] = getattr(
91+
settings, "TBXFORMS_HIGHLIGHT_REQUIRED_FIELDS", False
92+
)
8293

8394
return super().render_layout(
8495
form, context, template_pack=template_pack
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<form class="tbxforms" method="post">
2+
<div id="div_id_method" class="tbxforms-form-group">
3+
<fieldset class="tbxforms-fieldset" aria-describedby="id_method_hint">
4+
<legend class="tbxforms-fieldset__legend tbxforms-fieldset__legend--m">
5+
How would you like to be contacted?
6+
<span class="tbxforms-field_marker--optional">(optional)</span>
7+
</legend>
8+
<p id="id_method_hint" class="tbxforms-hint">Select all options that are relevant to you.</p>
9+
<div class="tbxforms-checkboxes">
10+
<div class="tbxforms-checkboxes__item">
11+
<input type="checkbox"
12+
name="method"
13+
class="tbxforms-checkboxes__input"
14+
id="id_method_1"
15+
value="email" />
16+
<label class="tbxforms-label tbxforms-checkboxes__label" for="id_method_1">Email</label>
17+
</div>
18+
<div class="tbxforms-checkboxes__item">
19+
<input type="checkbox"
20+
name="method"
21+
class="tbxforms-checkboxes__input"
22+
id="id_method_2"
23+
value="phone" />
24+
<label class="tbxforms-label tbxforms-checkboxes__label" for="id_method_2">Phone</label>
25+
</div>
26+
<div class="tbxforms-checkboxes__item">
27+
<input type="checkbox"
28+
name="method"
29+
class="tbxforms-checkboxes__input"
30+
id="id_method_3"
31+
value="text" />
32+
<label class="tbxforms-label tbxforms-checkboxes__label" for="id_method_3">Text message</label>
33+
</div>
34+
</div>
35+
</fieldset>
36+
</div>
37+
</form>

tests/layout/test_checkboxes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,14 @@ def test_required_field_highlighting(snapshot_html):
9797
"""
9898
form = CheckboxesForm()
9999
assert render_form(form) == snapshot_html
100+
101+
102+
@override_settings(TBXFORMS_HIGHLIGHT_REQUIRED_FIELDS=True)
103+
def test_required_field_highlighting_disabled_per_form(snapshot_html):
104+
"""
105+
Ensure that required field highlighting can be turned off per-form.
106+
"""
107+
form = CheckboxesForm()
108+
form.helper.highlight_required_fields = False
109+
form.fields["method"].required = False
110+
assert render_form(form) == snapshot_html

0 commit comments

Comments
 (0)