diff --git a/app/assets/javascripts/experiments/dropdown_actions.js b/app/assets/javascripts/experiments/dropdown_actions.js index 1034e7284..f6956737e 100644 --- a/app/assets/javascripts/experiments/dropdown_actions.js +++ b/app/assets/javascripts/experiments/dropdown_actions.js @@ -1,9 +1,10 @@ - (function(){ // Create ajax hook on given 'element', which should return modal with 'id' => // show that modal function initializeModal(element, id){ + + // Initializev new experiment modal listner $(element) .on("ajax:beforeSend", function(){ animateSpinner(); @@ -15,6 +16,7 @@ keyboard: false, }); validateMoveModal(id); + validateExperimentForm($(id)); }) .on("ajax:error", function() { animateSpinner(null, false); @@ -59,11 +61,42 @@ var msg = JSON.parse(error.responseText); renderFormError(e, form.find("#experiment_project_id"), - msg.message.toString()); + msg.message.toString(), + true); }) - .clearFormErrors(); } } + // Setup front-end validations for experiment form + function validateExperimentForm(element){ + if ( element ) { + var form = element.find("form"); + form + .on('ajax:success' , function(){ + animateSpinner(form, true); + location.reload(); + }) + .on('ajax:error', function(e, error){ + var msg = JSON.parse(error.responseText); + if ( 'name' in msg ) { + renderFormError(e, + element.find("#experiment-name"), + msg.name.toString(), + true); + } else if ( 'description' in msg ) { + renderFormError(e, + element.find("#experiment-description"), + msg.description.toString(), + true); + } else { + renderFormError(e, + element.find("#experiment-name"), + error.statusText, + true); + } + }) + } + } + // Initialize no description edit link function initEditNoDescription(){ var modal = "#edit-experiment-modal-"; diff --git a/app/assets/javascripts/sitewide/form_errors.js b/app/assets/javascripts/sitewide/form_errors.js index 8695dcf6d..5f9707cbc 100644 --- a/app/assets/javascripts/sitewide/form_errors.js +++ b/app/assets/javascripts/sitewide/form_errors.js @@ -33,9 +33,21 @@ $.fn.renderFormErrors = function (modelName, errors, clear, ev) { * and, if present, mark and show the tab where the error occured and * focus/scroll to the error input, if it is the first one to be * specified or if errMsgs is undefined. + * + * @param {string} errAttributes Span element (error) attributes + * @param {boolean} clearErr Set clearErr to true if this is the only + * error that can happen/show. */ -var renderFormError = function (ev, input, errMsgs, errAttributes) { +var renderFormError = function (ev, input, errMsgs, clearErr, errAttributes) { + clearErr = _.isUndefined(clearErr) ? false : clearErr; + errAttributes = _.isUndefined(errAttributes) ? "" : " " + errAttributes; + $form = $(input).closest("form"); + if (!_.isUndefined(errMsgs)) { + if (clearErr) { + $form.clearFormErrors(); + } + // Mark error form group $formGroup = $(input).closest(".form-group"); if (!$formGroup.hasClass("has-error")) { @@ -43,7 +55,6 @@ var renderFormError = function (ev, input, errMsgs, errAttributes) { } // Add error message/s - errAttributes = _.isUndefined(errAttributes) ? "" : " " + errAttributes; error_text = ($.makeArray(errMsgs).map(function (m) { return m.strToErrorFormat(); })).join("
"); @@ -51,7 +62,6 @@ var renderFormError = function (ev, input, errMsgs, errAttributes) { $formGroup.append($errSpan); } - $form = $(input).closest("form"); $tab = $(input).closest(".tab-pane"); if ($tab.length) { // Mark error tab diff --git a/app/assets/javascripts/sitewide/form_validators.js.erb b/app/assets/javascripts/sitewide/form_validators.js.erb index 8b5e93c0b..524390193 100644 --- a/app/assets/javascripts/sitewide/form_validators.js.erb +++ b/app/assets/javascripts/sitewide/form_validators.js.erb @@ -102,7 +102,7 @@ function filesPresentValidator(ev, fileInputs) { _.each(fileInputs, function(fileInput) { if (!fileInput.files[0]) { assetError = I18n.t("general.file.blank"); - renderFormError(ev, fileInput, assetError, "data-error='file-missing'"); + renderFormError(ev, fileInput, assetError, false, "data-error='file-missing'"); filesPresentValid = false; } }); @@ -132,7 +132,7 @@ function filesSizeValidator(ev, fileInputs, fileTypeEnum) { var file = fileInput.files[0]; var assetError = getFileTooBigError(file); if (assetError) { - renderFormError(ev, fileInput, assetError, "data-error='file-size'"); + renderFormError(ev, fileInput, assetError, false, "data-error='file-size'"); filesSizeValid = false; } }); diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index dedbac645..ed5b37785 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -39,10 +39,17 @@ class ExperimentsController < ApplicationController if @experiment.save flash[:success] = t('experiments.create.success_flash', experiment: @experiment.name) - redirect_to canvas_experiment_path(@experiment) + respond_to do |format| + format.json do + render json: {}, status: :ok + end + end else - flash[:alert] = t('experiments.create.error_flash') - redirect_to :back + respond_to do |format| + format.json do + render json: @experiment.errors, status: :unprocessable_entity + end + end end end @@ -69,10 +76,17 @@ class ExperimentsController < ApplicationController @experiment.touch(:workflowimg_updated_at) flash[:success] = t('experiments.update.success_flash', experiment: @experiment.name) - redirect_to canvas_experiment_path(@experiment) + respond_to do |format| + format.json do + render json: {}, status: :ok + end + end else - flash[:alert] = t('experiments.update.error_flash') - redirect_to :back + respond_to do |format| + format.json do + render json: @experiment.errors, status: :unprocessable_entity + end + end end end diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 5723fdea1..2717ebbf4 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -21,7 +21,7 @@ class Experiment < ActiveRecord::Base presence: true, length: { minimum: 4, maximum: 50 }, uniqueness: { scope: :project, case_sensitive: false } - validates :description, length: { maximum: 255 } + validates :description, length: { maximum: 50 } validates :project, presence: true validates :created_by, presence: true validates :last_modified_by, presence: true diff --git a/app/views/experiments/_edit_modal.html.erb b/app/views/experiments/_edit_modal.html.erb index b3a362e29..b2a1c3e85 100644 --- a/app/views/experiments/_edit_modal.html.erb +++ b/app/views/experiments/_edit_modal.html.erb @@ -4,7 +4,7 @@ tabindex="-1" role="dialog" aria-labelledby="edit-experiment-modal-label"> - <%= bootstrap_form_for [@project, @experiment] do |f| %> + <%= bootstrap_form_for [@project, @experiment], remote: true do |f| %>