mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-26 08:09:51 -04:00
Don’t reformat numbers if there are errors
It’s confusing to at the same time: 1. change what you’ve inputted 2. tell you it’s wrong This commit makes it so that 1. only happens if 2. doesn’t.
This commit is contained in:
@@ -181,17 +181,17 @@ class ForgivingIntegerField(StringField):
|
||||
|
||||
if valuelist:
|
||||
|
||||
valuelist[0] = valuelist[0].replace(',', '').replace(' ', '')
|
||||
value = valuelist[0].replace(',', '').replace(' ', '')
|
||||
|
||||
try:
|
||||
valuelist[0] = int(valuelist[0])
|
||||
value = int(value)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if valuelist[0] == '':
|
||||
valuelist[0] = 0
|
||||
if value == '':
|
||||
value = 0
|
||||
|
||||
return super().process_formdata(valuelist)
|
||||
return super().process_formdata([value])
|
||||
|
||||
def pre_validate(self, form):
|
||||
|
||||
@@ -210,6 +210,12 @@ class ForgivingIntegerField(StringField):
|
||||
|
||||
def __call__(self, **kwargs):
|
||||
|
||||
if self.get_form().is_submitted() and not self.get_form().validate():
|
||||
return super().__call__(
|
||||
value=(self.raw_data or [None])[0],
|
||||
**kwargs
|
||||
)
|
||||
|
||||
try:
|
||||
value = int(self.data)
|
||||
value = '{:,.0f}'.format(value)
|
||||
|
||||
@@ -968,6 +968,9 @@ def test_should_error_if_all_volumes_zero(
|
||||
},
|
||||
_expected_status=200,
|
||||
)
|
||||
assert page.select('input[type=text]')[0]['value'] == ''
|
||||
assert page.select('input[type=text]')[1]['value'] == '0'
|
||||
assert page.select('input[type=text]')[2]['value'] == '0,00 0'
|
||||
assert normalize_spaces(page.select_one('.banner-dangerous').text) == (
|
||||
'no things supplied '
|
||||
'Tell us some things'
|
||||
@@ -975,6 +978,34 @@ def test_should_error_if_all_volumes_zero(
|
||||
assert mock_update_service.called is False
|
||||
|
||||
|
||||
def test_should_not_default_to_zero_if_some_fields_dont_validate(
|
||||
client_request,
|
||||
mock_update_service,
|
||||
):
|
||||
page = client_request.post(
|
||||
'main.estimate_usage',
|
||||
service_id=SERVICE_ONE_ID,
|
||||
_data={
|
||||
'volume_email': '1234',
|
||||
'volume_sms': '',
|
||||
'volume_letter': 'aaaaaaaaaaaaa',
|
||||
'consent_to_research': 'yes',
|
||||
},
|
||||
_expected_status=200,
|
||||
)
|
||||
assert page.select('input[type=text]')[0]['value'] == '1234'
|
||||
assert page.select('input[type=text]')[1]['value'] == ''
|
||||
assert page.select('input[type=text]')[2]['value'] == 'aaaaaaaaaaaaa'
|
||||
assert normalize_spaces(
|
||||
page.select_one('label[for=volume_letter]').text
|
||||
) == (
|
||||
'How many letters do you expect to send in the next year? '
|
||||
'For example, 5,000 '
|
||||
'Must be a whole number'
|
||||
)
|
||||
assert mock_update_service.called is False
|
||||
|
||||
|
||||
def test_non_gov_users_cant_request_to_go_live(
|
||||
client_request,
|
||||
api_nongov_user_active,
|
||||
|
||||
Reference in New Issue
Block a user