Skip to content

Commit 09a544f

Browse files
Themitchellmiriam-z
authored andcommitted
PPHA-263: Limit MetricWeightForm decimal places to 1
1 parent 0fd0815 commit 09a544f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lung_cancer_screening/questions/forms/metric_weight_form.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ def __init__(self, *args, **kwargs):
1111
self.instance.participant = self.participant
1212

1313
self.fields["weight_metric"] = DecimalField(
14+
decimal_places=1,
1415
label="Kilograms",
1516
classes="nhsuk-input--width-4",
1617
required=True,
1718
error_messages={
1819
'required': 'Enter your weight',
20+
'max_decimal_places': 'Kilograms must be to 1 decimal place, for example 90.2kgs',
1921
},
2022
suffix="kg"
2123
)
24+
2225
def clean_weight_metric(self):
2326
return self.cleaned_data['weight_metric'] * 10
2427

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,17 @@ def test_accepts_maximum_valid_weight(self):
100100
}
101101
)
102102
self.assertTrue(form.is_valid())
103+
104+
def test_is_invalid_with_multiple_decimal_places(self):
105+
form = MetricWeightForm(
106+
participant=self.participant,
107+
instance=self.response_set,
108+
data={
109+
"weight_metric": "100.01" # too many decimal places
110+
}
111+
)
112+
self.assertFalse(form.is_valid())
113+
self.assertEqual(
114+
form.errors["weight_metric"],
115+
["Kilograms must be to 1 decimal place, for example 90.2kgs"]
116+
)

0 commit comments

Comments
 (0)