Skip to content

Commit 6377cb3

Browse files
committed
PPHA-262: Ensure all validation errors are correct for height form
1 parent 9a48599 commit 6377cb3

File tree

7 files changed

+68
-31
lines changed

7 files changed

+68
-31
lines changed

lung_cancer_screening/core/form_fields.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,23 @@ class ImperialHeightField(forms.MultiValueField):
410410
widget = ImperialHeightWidget
411411

412412
def __init__(self, *args, **kwargs):
413+
error_messages = kwargs.get("error_messages", {})
414+
415+
feet_kwargs = {
416+
"error_messages": {
417+
'invalid': 'Feet must be in whole numbers.',
418+
**error_messages,
419+
},
420+
}
421+
inches_kwargs = {
422+
"error_messages": {
423+
'invalid': 'Inches must be in whole numbers.',
424+
**error_messages,
425+
},
426+
}
413427
fields = (
414-
IntegerField(label="Feet"),
415-
IntegerField(label="Inches"),
428+
IntegerField(**feet_kwargs),
429+
IntegerField(**inches_kwargs),
416430
)
417431
kwargs["template_name"] = "forms/imperial-height-input.jinja"
418432

lung_cancer_screening/core/jinja2/forms/imperial-height-input.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"hint": {
2828
"text": unbound_field.hint
2929
} if unbound_field.hint,
30-
"id": field.auto_id,
30+
"id": field.auto_id + "_0",
3131
"name": field.html_name + "_0",
3232
"value": field.subwidgets.0.data.value,
3333
"classes": "nhsuk-input--width-2" + field_error_classes,

lung_cancer_screening/core/tests/acceptance/test_questionnaire_validation_errors.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,39 @@ def test_height_validation_errors(self):
4747
page.goto(f"{self.live_server_url}/height")
4848

4949
page.click("text=Continue")
50-
5150
expect(page.locator(".nhsuk-error-message")).to_contain_text(
5251
"Enter your height."
5352
)
5453

54+
page.get_by_label("Centimetre").fill('139.6')
55+
page.click('text=Continue')
56+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
57+
"Height must be between 139.7cm and 243.8 cm"
58+
)
59+
60+
page.get_by_label("Centimetre").fill('243.9')
61+
page.click('text=Continue')
62+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
63+
"Height must be between 139.7cm and 243.8 cm"
64+
)
65+
5566
page.click("text=Switch to imperial")
5667

5768
page.click("text=Continue")
69+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
70+
"Enter your height."
71+
)
5872

59-
for error in page.locator(".nhsuk-error-message").all():
60-
expect(error).to_contain_text(
61-
"Enter your height."
62-
)
73+
page.get_by_label("Feet").fill('5.2')
74+
page.get_by_label("Inches").fill('2')
75+
page.click('text=Continue')
76+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
77+
"Feet must be in whole numbers"
78+
)
79+
80+
page.get_by_label("Feet").fill('5')
81+
page.get_by_label("Inches").fill('2.2')
82+
page.click('text=Continue')
83+
expect(page.locator(".nhsuk-error-message")).to_contain_text(
84+
"Inches must be in whole numbers"
85+
)

lung_cancer_screening/questions/forms/imperial_height_form.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def __init__(self, *args, **kwargs):
1515
required=True,
1616
require_all_fields=False,
1717
error_messages={
18-
'required': 'Enter your height.'
18+
'required': 'Enter your height.',
19+
'incomplete': 'Enter your height.'
1920
}
2021
)
2122

lung_cancer_screening/questions/jinja2/height.jinja

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
{{ form.height.as_field_group() }}
4040
{% endif %}
4141

42-
<a href="?unit={{ switch_to_unit }}">Switch to {{ switch_to_unit }}</a>
42+
43+
<p><a href="?unit={{ switch_to_unit }}">Switch to {{ switch_to_unit }}</a></p>
4344

4445
{% endcall %}
4546

lung_cancer_screening/questions/tests/unit/forms/test_imperial_height_form.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,22 @@ def test_is_invalid_with_missing_data(self):
5858
}
5959
)
6060
self.assertFalse(form.is_valid())
61+
self.assertEqual(
62+
form.errors["height_imperial"],
63+
["Enter your height."]
64+
)
65+
66+
def test_is_invalid_when_given_a_decimal_feet_value(self):
67+
form = ImperialHeightForm(
68+
participant=self.participant,
69+
instance=self.response_set,
70+
data={
71+
"height_imperial_0": "5.2",
72+
"height_imperial_1": "0"
73+
}
74+
)
75+
self.assertFalse(form.is_valid())
76+
self.assertEqual(
77+
form.errors["height_imperial"],
78+
["Feet must be in whole numbers."]
79+
)

scripts/tests/ui.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)