diff --git a/lung_cancer_screening/questions/forms/metric_weight_form.py b/lung_cancer_screening/questions/forms/metric_weight_form.py index 8b4a8199..dd7d2fea 100644 --- a/lung_cancer_screening/questions/forms/metric_weight_form.py +++ b/lung_cancer_screening/questions/forms/metric_weight_form.py @@ -11,14 +11,17 @@ def __init__(self, *args, **kwargs): self.instance.participant = self.participant self.fields["weight_metric"] = DecimalField( + decimal_places=1, label="Kilograms", classes="nhsuk-input--width-4", required=True, error_messages={ 'required': 'Enter your weight', + 'max_decimal_places': 'Kilograms must be to 1 decimal place, for example 90.2kgs', }, suffix="kg" ) + def clean_weight_metric(self): return self.cleaned_data['weight_metric'] * 10 diff --git a/lung_cancer_screening/questions/tests/unit/forms/test_metric_weight_form.py b/lung_cancer_screening/questions/tests/unit/forms/test_metric_weight_form.py index d4d89a7e..49b1cdfc 100644 --- a/lung_cancer_screening/questions/tests/unit/forms/test_metric_weight_form.py +++ b/lung_cancer_screening/questions/tests/unit/forms/test_metric_weight_form.py @@ -100,3 +100,17 @@ def test_accepts_maximum_valid_weight(self): } ) self.assertTrue(form.is_valid()) + + def test_is_invalid_with_multiple_decimal_places(self): + form = MetricWeightForm( + participant=self.participant, + instance=self.response_set, + data={ + "weight_metric": "100.01" # too many decimal places + } + ) + self.assertFalse(form.is_valid()) + self.assertEqual( + form.errors["weight_metric"], + ["Kilograms must be to 1 decimal place, for example 90.2kgs"] + )