Skip to content

Commit be01b23

Browse files
authored
Allow disabling error summaries and tweak fieldset styles. (#93)
1 parent c631791 commit be01b23

File tree

10 files changed

+114
-6
lines changed

10 files changed

+114
-6
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
with:
1515
node-version-file: '.nvmrc'
1616
- id: npm-cache
17-
uses: actions/cache@v2
17+
uses: actions/cache@v4
1818
with:
1919
path: node_modules
2020
key: ${{ runner.os }}-node-${{ hashFiles('.nvmrc') }}-${{ hashFiles('**/package-lock.json') }}
@@ -41,7 +41,7 @@ jobs:
4141
virtualenvs-create: true
4242
virtualenvs-in-project: true
4343
- id: poetry-cache
44-
uses: actions/cache@v2
44+
uses: actions/cache@v4
4545
with:
4646
path: .venv
4747
key: ${{ runner.os }}-python-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,46 @@ You can also style these markers by targeting these CSS classes:
404404

405405
### Change the default label and legend classes
406406

407+
Label and legend sizes can be changed through the form's helper, e.g.:
408+
409+
```python
410+
from tbxforms.layout import Size
411+
412+
class ExampleForm(...):
413+
...
414+
415+
def __init__(self, *args, **kwargs):
416+
super().__init__(*args, **kwargs)
417+
self.helper.label_size = Size.LARGE
418+
self.helper.legend_size = Size.LARGE
419+
```
420+
407421
Possible values for the `label_size` and `legend_size`:
408422

409423
1. `SMALL`
410424
2. `MEDIUM` (default)
411425
3. `LARGE`
412426
4. `EXTRA_LARGE`
413427

428+
### Disable error summary
429+
430+
You can disable the [error summary](https://design-system.service.gov.uk/components/error-summary/)
431+
by setting `show_error_summary=False` in the form's helper, e.g.:
432+
433+
```python
434+
class ExampleForm(...):
435+
...
436+
437+
def __init__(self, *args, **kwargs):
438+
super().__init__(*args, **kwargs)
439+
self.helper.show_error_summary = False
440+
```
441+
442+
This is useful if your form has a complex setup and you need complete control
443+
over the error summary - e.g. if the form also contains a formset. In this
444+
instance, you would want to create your own error summary template and include
445+
it in your template.
446+
414447
# Further reading
415448

416449
- Download the [PyPI package](http://pypi.python.org/pypi/tbxforms)

tbxforms/helper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self, *args, **kwargs):
5757

5858
label_size = ""
5959
legend_size = ""
60+
show_error_summary = True
6061

6162
def render_layout(self, form, context, template_pack=TEMPLATE_PACK):
6263
"""

tbxforms/static/sass/govuk/components/fieldset/_index.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@include tbxforms-exports("govuk/component/fieldset") {
22
.tbxforms-fieldset {
33
min-width: 0;
4-
margin: 0;
54
padding: 0;
65
border: 0;
76
@include tbxforms-clearfix;

tbxforms/static/sass/govuk/objects/_form-group.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@include tbxforms-clearfix;
44
@include tbxforms-responsive-margin(6, "bottom");
55

6-
.tbxforms-form-group:last-of-type {
6+
.tbxforms-form-group:last-child {
77
margin-bottom: 0; // Remove margin from last item in nested groups
88
}
99
}

tbxforms/templates/tbxforms/display_form.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{% if form.form_html %}
22
{% if include_media %}{{ form.media }}{% endif %}
3-
{% include "tbxforms/errors.html" %}
3+
4+
{% if form.helper.show_error_summary %}
5+
{% include "tbxforms/errors.html" %}
6+
{% endif %}
7+
48
{{ form.form_html }}
59
{% else %}
610
{% include "tbxforms/uni_form.html" %}

tbxforms/templates/tbxforms/uni_form.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{% if include_media %}{{ form.media }}{% endif %}
22

3-
{% include "tbxforms/errors.html" %}
3+
{% if form.helper.show_error_summary %}
4+
{% include "tbxforms/errors.html" %}
5+
{% endif %}
46

57
{% for field in form %}
68
{% include field_template %}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<form class="tbxforms" method="post">
2+
<div id="div_id_accept" class="tbxforms-form-group">
3+
<p id="id_accept_hint" class="tbxforms-hint">Please read the terms of service.</p>
4+
<div class="tbxforms-checkboxes">
5+
<div class="tbxforms-checkboxes__item">
6+
<input type="checkbox"
7+
name="accept"
8+
aria-describedby="id_accept_hint"
9+
class="tbxforms-checkboxes__input"
10+
required="required"
11+
id="id_accept"
12+
checked>
13+
<label class="tbxforms-label tbxforms-checkboxes__label" for="id_accept">I accept the terms of service</label>
14+
</div>
15+
</div>
16+
</div>
17+
</form>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<form class="tbxforms" method="post">
2+
<div class="tbxforms-error-summary"
3+
aria-labelledby="error-summary-title"
4+
role="alert"
5+
tabindex="-1">
6+
<h2 class="tbxforms-error-summary__title" id="error-summary-title">There is a problem with your submission</h2>
7+
<div class="tbxforms-error-summary__body">
8+
<ul class="tbxforms-list tbxforms-error-summary__list">
9+
<li class="tbxforms-error-summary__list__item">An example non-field error.</li>
10+
</ul>
11+
</div>
12+
</div>
13+
<div id="div_id_accept" class="tbxforms-form-group">
14+
<p id="id_accept_hint" class="tbxforms-hint">Please read the terms of service.</p>
15+
<div class="tbxforms-checkboxes">
16+
<div class="tbxforms-checkboxes__item">
17+
<input type="checkbox"
18+
name="accept"
19+
aria-describedby="id_accept_hint"
20+
class="tbxforms-checkboxes__input"
21+
required="required"
22+
id="id_accept"
23+
checked>
24+
<label class="tbxforms-label tbxforms-checkboxes__label" for="id_accept">I accept the terms of service</label>
25+
</div>
26+
</div>
27+
</div>
28+
</form>

tests/helpers/test_form_helper.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010
from tests.forms import (
1111
CheckboxesForm,
12+
CheckboxForm,
1213
TextInputForm,
1314
)
1415
from tests.utils import render_form
@@ -44,3 +45,26 @@ def test_override_default_legend_size(snapshot_html):
4445
Field.checkboxes("method", legend_size=Size.LARGE)
4546
)
4647
assert render_form(form) == snapshot_html
48+
49+
50+
def test_error_summary_shown_by_default(snapshot_html):
51+
"""Verify the error summary is shown by default."""
52+
form = CheckboxForm(data={"accept": True})
53+
form.add_error(None, "An example non-field error.")
54+
assert not form.is_valid()
55+
56+
rendered_form = render_form(form)
57+
assert rendered_form == snapshot_html
58+
assert "There is a problem with your submission" in rendered_form
59+
60+
61+
def test_error_summary_can_be_hidden(snapshot_html):
62+
"""Verify the error summary can be hidden."""
63+
form = CheckboxForm(data={"accept": True})
64+
form.add_error(None, "An example non-field error.")
65+
form.helper.show_error_summary = False
66+
assert not form.is_valid()
67+
68+
rendered_form = render_form(form)
69+
assert rendered_form == snapshot_html
70+
assert "There is a problem with your submission" not in rendered_form

0 commit comments

Comments
 (0)