From 21e8ae4990a4cd422bf917804dedf8a0a1ef7070 Mon Sep 17 00:00:00 2001 From: Anna Malantonio Date: Thu, 10 Feb 2022 15:02:58 -0500 Subject: [PATCH] add presence validation for workflow_actions that require comments --- app/assets/javascripts/application.js | 2 +- .../workflow-action-form-validation.js | 45 +++++++++++++++++++ .../javascripts/workflow-action-form.js | 22 --------- 3 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 app/assets/javascripts/workflow-action-form-validation.js delete mode 100644 app/assets/javascripts/workflow-action-form.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index fd0f0d75b..6d02327e3 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -31,4 +31,4 @@ //= require spot/editor/multi_auth_controlled_vocabulary //= require language-tagged-autocomplete-setup //= require multi-auth-input -//= require workflow-action-form +//= require workflow-action-form-validation diff --git a/app/assets/javascripts/workflow-action-form-validation.js b/app/assets/javascripts/workflow-action-form-validation.js new file mode 100644 index 000000000..210ade9e5 --- /dev/null +++ b/app/assets/javascripts/workflow-action-form-validation.js @@ -0,0 +1,45 @@ +// some basic validations for the workflow_controls form +$(document).ready(function () { + $('#workflow_controls form').on('submit', function (ev) { + // close out any previous errors + $('.workflow-action-validation-error').alert('close'); + + // these workflow_actions require comments + var workflowActionsRequiringComments = [ + 'advisor_requests_changes', + 'library_requests_changes', + ]; + var actionName = $('#workflow_controls form input[name="workflow_action[name]"]:checked').val(); + var requiresComment = workflowActionsRequiringComments.indexOf(actionName) > -1; + var comment = $('#workflow_action_comment').val().trim(); + var alertText; + + // an action has been provided, let's validate whether or not it needs a comment as well + if (actionName) { + // no comments required OR comments are required and one is present, + // break out of this function to submit the form + if (!requiresComment || (requiresComment && comment)) { return; } + + // otherwise we need a comment, so we'll display the alert + else { alertText = 'Please provide a comment about what changes are desired.'; } + } + + // no action has been provided, we need one + else { + alertText = 'Please select an action below.'; + } + + // stop the submission + insert an alert for the user + ev.preventDefault(); + + var $alert = $('') + .append('') + .append(alertText); + + $(this).find('.panel-body').prepend($alert); + + return false; + }); +}) diff --git a/app/assets/javascripts/workflow-action-form.js b/app/assets/javascripts/workflow-action-form.js deleted file mode 100644 index 25cea696e..000000000 --- a/app/assets/javascripts/workflow-action-form.js +++ /dev/null @@ -1,22 +0,0 @@ -// prevent the workflow-action form from being submitted if no action is selected -$(document).ready(function () { - $('#workflow_controls form').on('submit', function (ev) { - var $actionName = $('#workflow_controls form input[name="workflow_action[name]"]:checked').val(); - - // submit the form if we have an action to take - if ($actionName) { return; } - - // stop the submission + insert an alert for the user - ev.preventDefault(); - - var $alert = $('') - .append('') - .append('Please select an action below'); - - $(this).find('.panel-body').prepend($alert); - - return false; - }); -})