From 3dabd39c412f8fc146dcc8cb08958e40872eba40 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Fri, 3 Mar 2017 16:15:15 +0000 Subject: [PATCH] Add better error if user goes over line limit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don’t make people count the number of lines themselves. --- app/main/forms.py | 7 +++++-- tests/app/main/views/test_service_settings.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index 3ba8894c2..5a5138dd7 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -496,8 +496,11 @@ class ServiceLetterContactBlock(Form): ) def validate_letter_contact_block(form, field): - if field.data.strip().count('\n') >= 10: - raise ValidationError('Can only have 10 lines') + line_count = field.data.strip().count('\n') + if line_count >= 10: + raise ValidationError( + 'Contains {} lines, maximum is 10'.format(line_count + 1) + ) class ServiceBrandingOrg(Form): diff --git a/tests/app/main/views/test_service_settings.py b/tests/app/main/views/test_service_settings.py index f35eda093..9fd2b4252 100644 --- a/tests/app/main/views/test_service_settings.py +++ b/tests/app/main/views/test_service_settings.py @@ -627,12 +627,12 @@ def test_set_letter_contact_block_has_max_10_lines( service_one['can_send_letters'] = True response = logged_in_client.post( url_for('main.service_set_letter_contact_block', service_id=service_one['id']), - data={'letter_contact_block': '\n'.join(map(str, range(0, 12)))} + data={'letter_contact_block': '\n'.join(map(str, range(0, 11)))} ) assert response.status_code == 200 page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') error_message = page.find('span', class_='error-message').text.strip() - assert error_message == 'Can only have 10 lines' + assert error_message == 'Contains 11 lines, maximum is 10' def test_should_show_branding(