From 21452649fdace2ba16778e3213ff59b09e8e4527 Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Mon, 7 Mar 2022 12:40:18 +0000 Subject: [PATCH] Fix ignoring submitted data that was falsey This was causing some tests for the "estimate_volume" endpoint to fail due to the surprising way that form handles "''": - The form is the exclusive user of the ForgivingIntegerField [^1]. - The field secretly/silently converts "''" to the integer 0 [^2]. If the validations fail, we don't want to surprise the user with a "0" when they didn't enter one. The field already handles this by massaging the values in the __call__ method that generates the HTML for the form [^3]. However, there are two scenarios: - User submits field with '' - converted to integer 0. - User submits field with '0' - remains as a string. In the case where "value" is "''", the parent class will use the converted value from form.data instead [^4]. This seems to be an oversight and so we get either the integer 0 (from form.data) or the string '0' (from the value kwarg). Complicado! Previously it was a fluke that we avoided replaying the integer 0 to the user; the previous commit removes the fluke. This fixes the conditional to always use the data in the "value" kwarg if it has been provided, as it's meant to override "form.data". [^1]: https://github.com/alphagov/notifications-admin/commit/9f634493841d3dad54ca709f4a688dabb207fd15 [^2]: https://github.com/alphagov/notifications-admin/blob/a22b8cf684b5581aad54d6e36d98e498085925a5/app/main/forms.py#L364 [^3]: https://github.com/alphagov/notifications-admin/blob/a22b8cf684b5581aad54d6e36d98e498085925a5/app/main/forms.py#L393 [^4]: https://github.com/alphagov/notifications-admin/commit/a22b8cf684b5581aad54d6e36d98e498085925a5#diff-a1c8d24b22d4478fe71f75fd43b71b18dd82aae97bc63de84473a6da1902909bR215 --- app/main/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main/forms.py b/app/main/forms.py index 4fa538c3c..3b83b01cc 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -221,7 +221,7 @@ def password(label='Password'): def govuk_text_input_field_widget(self, field, type=None, param_extensions=None, **kwargs): - value = kwargs["value"] if kwargs.get("value") else field.data + value = kwargs["value"] if "value" in kwargs else field.data value = str(value) if isinstance(value, Number) else value # error messages