fix being able to add template without selecting a radio button

This commit is contained in:
Leo Hemsted
2018-12-07 16:38:48 +00:00
parent 180654046a
commit 6f8dc7e6d3
3 changed files with 13 additions and 6 deletions

View File

@@ -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('Cant be empty')
raise validators.StopValidation('Must be empty')
if form.op in operations and not any(field.raw_data):
raise validators.StopValidation('Cant 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')
])

View File

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

View File

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