From 9dc3252079ac4e9fe6ff9e0d25371daeecfe7d27 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Fri, 25 Feb 2022 11:27:56 +0000 Subject: [PATCH] Allow free allowance to be set to 0 We want to be able to set the free allowance for a service to 0, but the form was not allowing this - it gave an error message of `Cannot be empty`. This can be fixed by changing the WTForms validator from `DataRequired` (which coerces 0 to falsey) to the `InputRequired` validator. --- app/main/forms.py | 11 +++++++++-- tests/app/main/views/test_service_settings.py | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index f6f7412a2..2c89859a4 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -40,7 +40,14 @@ from wtforms import ( ValidationError, validators, ) -from wtforms.validators import URL, DataRequired, Length, Optional, Regexp +from wtforms.validators import ( + URL, + DataRequired, + InputRequired, + Length, + Optional, + Regexp, +) from app.formatters import format_thousands, guess_name_from_email_address from app.main.validators import ( @@ -1300,7 +1307,7 @@ class FreeSMSAllowance(StripWhitespaceForm): free_sms_allowance = GovukIntegerField( 'Numbers of text message fragments per year', validators=[ - DataRequired(message='Cannot be empty') + InputRequired(message='Cannot be empty') ] ) diff --git a/tests/app/main/views/test_service_settings.py b/tests/app/main/views/test_service_settings.py index 1ead2e1f4..9d22a36ec 100644 --- a/tests/app/main/views/test_service_settings.py +++ b/tests/app/main/views/test_service_settings.py @@ -3800,6 +3800,7 @@ def test_should_show_page_to_set_sms_allowance( @freeze_time("2017-04-01 11:09:00.061258") @pytest.mark.parametrize('given_allowance, expected_api_argument', [ + ('0', 0), ('1', 1), ('250000', 250000), pytest.param('foo', 'foo', marks=pytest.mark.xfail),