From de6c03a73bf40173a86dc6751517166866d146e3 Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 1 Sep 2016 16:52:42 +0200 Subject: [PATCH 1/8] setup experiment modal input validation --- .../javascripts/experiments/dropdown_actions.js | 12 +++++++++++- app/views/experiments/_form.html.erb | 10 ++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/experiments/dropdown_actions.js b/app/assets/javascripts/experiments/dropdown_actions.js index 1034e7284..8b5925df8 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(); }) .on("ajax:error", function() { animateSpinner(null, false); @@ -63,6 +65,14 @@ }) .clearFormErrors(); } + // Setup front-end validations for experiment form + function validateExperimentForm(){ + var form = $("#new-experiment-modal").find("form"); + form.on('submit', function(ev){ + textValidator(ev, $('#new-experiment-name'), false); + textValidator(ev, $('#new-experiment-description'), true); + }); + form.clearFormErrors(); } // Initialize no description edit link function initEditNoDescription(){ diff --git a/app/views/experiments/_form.html.erb b/app/views/experiments/_form.html.erb index 29732c85f..ab78ede99 100644 --- a/app/views/experiments/_form.html.erb +++ b/app/views/experiments/_form.html.erb @@ -1,7 +1,11 @@
- <%= form.text_field :name, label: t("experiments.new.name"), autofocus: true, placeholder: t("experiments.new.name_placeholder") %> + <%= form.text_field :name, + label: t("experiments.new.name"), + autofocus: true, + placeholder: t("experiments.new.name_placeholder"), + id: 'new-experiment-name'%>
@@ -9,7 +13,9 @@
- <%= form.text_area :description, label: t('experiments.new.description') %> + <%= form.text_area :description, + label: t('experiments.new.description'), + id: 'new-experiment-description' %>
From 9d469f90922faa0199007454a4e49e52add86bd8 Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 2 Sep 2016 11:46:48 +0200 Subject: [PATCH 2/8] sets front end validation for new experiment modal [fixes SCI-348] --- .../experiments/dropdown_actions.js | 27 ++++++++++++++++--- app/controllers/experiments_controller.rb | 13 ++++++--- app/views/experiments/_new_modal.html.erb | 2 +- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/experiments/dropdown_actions.js b/app/assets/javascripts/experiments/dropdown_actions.js index 8b5925df8..efa7ae741 100644 --- a/app/assets/javascripts/experiments/dropdown_actions.js +++ b/app/assets/javascripts/experiments/dropdown_actions.js @@ -68,11 +68,32 @@ // Setup front-end validations for experiment form function validateExperimentForm(){ var form = $("#new-experiment-modal").find("form"); - form.on('submit', function(ev){ + 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, + $('#new-experiment-name'), + msg.name.toString()); + } else if ( 'description' in msg ) { + renderFormError(e, + $('#new-experiment-description'), + msg.description.toString()); + } else { + renderFormError(e, + $('#new-experiment-name'), + error.statusText); + } + }) + .on('submit', function(ev) { textValidator(ev, $('#new-experiment-name'), false); textValidator(ev, $('#new-experiment-description'), true); - }); - form.clearFormErrors(); + }) + .clearFormErrors(); } // Initialize no description edit link function initEditNoDescription(){ diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index dedbac645..16a70aef9 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 diff --git a/app/views/experiments/_new_modal.html.erb b/app/views/experiments/_new_modal.html.erb index 598f202a4..959786049 100644 --- a/app/views/experiments/_new_modal.html.erb +++ b/app/views/experiments/_new_modal.html.erb @@ -1,6 +1,6 @@