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]: 9f63449384
[^2]: a22b8cf684/app/main/forms.py (L364)
[^3]: a22b8cf684/app/main/forms.py (L393)
[^4]: a22b8cf684 (diff-a1c8d24b22d4478fe71f75fd43b71b18dd82aae97bc63de84473a6da1902909bR215)
This commit is contained in:
Ben Thorner
2022-03-07 12:40:18 +00:00
parent 09899eb99d
commit 21452649fd

View File

@@ -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