Skip to content

Commit 19d225c

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

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ allows you to alter the rendering behaviour of forms.
197197
Every form that inherits from `TbxFormsMixin` (i.e. every form within `tbxforms`)
198198
will have a `FormHelper` with the following default attributes:
199199

200+
- `highlight_required_fields`: see [later section on highlighting required fields](#highlight-required-fields-instead-of-optional-ones)
200201
- `html5_required = True`
201202
- `label_size = Size.MEDIUM`
202203
- `legend_size = Size.MEDIUM`
@@ -397,6 +398,20 @@ recommended by GDS.
397398
If `TBXFORMS_HIGHLIGHT_REQUIRED_FIELDS=True`, required fields will have an
398399
asterisk appended to their labels and optional fields will not be highlighted.
399400

401+
This setting can be changed on a per-form basis by setting the form helper's
402+
`highlight_required_fields` attribute:
403+
```python
404+
from django import forms
405+
from tbxforms.forms import TbxFormsMixin
406+
407+
408+
class ExampleForm(TbxFormsMixin, forms.Form):
409+
def __init__(self, *args, **kwargs):
410+
super().__init__(*args, **kwargs)
411+
# Highlight required fields regardless of settings.TBXFORMS_HIGHLIGHT_REQUIRED_FIELDS
412+
self.helper.highlight_required_fields = True
413+
```
414+
400415
You can also style these markers by targeting these CSS classes:
401416

402417
- `.tbxforms-field_marker--required`

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)