change error message for not selecting a template

needed to subclass RadioField for this
This commit is contained in:
Leo Hemsted
2018-12-07 17:09:15 +00:00
parent 6f8dc7e6d3
commit 5c3c4ec78c
2 changed files with 38 additions and 2 deletions

View File

@@ -736,6 +736,18 @@ class HiddenFieldWithNoneOption(FieldWithNoneOption, HiddenField):
pass
class RadioFieldWithRequiredMessage(RadioField):
def __init__(self, *args, required_message='Not a valid choice', **kwargs):
self.required_message = required_message
super().__init__(*args, **kwargs)
def pre_validate(self, form):
try:
return super().pre_validate(form)
except ValueError:
raise ValueError(self.required_message)
class ServiceSetBranding(StripWhitespaceForm):
branding_style = RadioFieldWithNoneOption(
@@ -1247,7 +1259,7 @@ class TemplateAndFoldersSelectionForm(Form):
add_new_folder_name = StringField('Folder name', validators=[required_for_ops('add-new-folder')])
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=[
add_template_by_template_type = RadioFieldWithRequiredMessage('Add new', validators=[
required_for_ops('add-new-template'),
Optional(),
])
], required_message='Select the type of template you want to add')

View File

@@ -1092,3 +1092,27 @@ def test_should_be_able_to_move_to_new_folder(
folder_ids={FOLDER_TWO_ID},
template_ids={TEMPLATE_ONE_ID},
)
def test_radio_button_with_no_value_shows_custom_error_message(
client_request,
service_one,
mock_get_service_templates,
mock_get_template_folders,
mock_move_to_template_folder,
mock_create_template_folder,
):
service_one['permissions'] += ['edit_folders']
page = client_request.post(
'main.choose_template',
service_id=SERVICE_ONE_ID,
_data={'operation': 'add-new-template'},
_expected_status=200,
_expected_redirect=None,
)
assert mock_move_to_template_folder.called is False
assert mock_create_template_folder.called is False
assert page.select_one('span.error-message').text.strip() == 'Select the type of template you want to add'