From 6f8dc7e6d3192d5631418b78248e6a87140acd73 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Fri, 7 Dec 2018 16:38:48 +0000 Subject: [PATCH] fix being able to add template without selecting a radio button --- app/main/forms.py | 12 ++++++------ app/main/views/templates.py | 3 +++ tests/app/main/views/test_template_folders.py | 4 ++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index ccb11dae5..472ead468 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -1151,17 +1151,17 @@ def required_for_ops(*operations): operations = set(operations) def validate(form, field): - if form.op not in operations and field.data: + if form.op not in operations and any(field.raw_data): # super weird - raise ValidationError('Must be empty') - if form.op in operations and not field.data: - raise ValidationError('Can’t be empty') + raise validators.StopValidation('Must be empty') + if form.op in operations and not any(field.raw_data): + raise validators.StopValidation('Can’t be empty') return validate class TemplateAndFoldersSelectionForm(Form): """ - This form also expects the form data to include an operation, based on which submit button is clicked. + This form expects the form data to include an operation, based on which submit button is clicked. If enter is pressed, unknown will be sent by a hidden submit button at the top of the form. The value of this operation affects which fields are required, expected to be empty, or optional. @@ -1248,6 +1248,6 @@ class TemplateAndFoldersSelectionForm(Form): move_to_new_folder_name = StringField('Folder name', validators=[required_for_ops('move-to-new-folder')]) add_template_by_template_type = RadioField('Add new', validators=[ + required_for_ops('add-new-template'), Optional(), - required_for_ops('add-new-template') ]) diff --git a/app/main/views/templates.py b/app/main/views/templates.py index fc15241b4..986b949d5 100644 --- a/app/main/views/templates.py +++ b/app/main/views/templates.py @@ -130,6 +130,9 @@ def choose_template(service_id, template_type='all', template_folder_id=None): except Exception as e: flash(e.message) + if 'templates_and_folders' in templates_and_folders_form.errors: + flash('Select at least one template or folder') + return render_template( 'views/templates/choose.html', current_template_folder_id=template_folder_id, diff --git a/tests/app/main/views/test_template_folders.py b/tests/app/main/views/test_template_folders.py index 977d77994..4b41617fd 100644 --- a/tests/app/main/views/test_template_folders.py +++ b/tests/app/main/views/test_template_folders.py @@ -959,6 +959,10 @@ def test_should_be_able_to_move_a_sub_item( 'move_to': '__NONE__', 'add_template_by_template_type': 'email', }, + # add a new template, but don't select anything + { + 'operation': 'add-new-template', + }, ]) def test_no_action_if_user_fills_in_ambiguous_fields( client_request,