From 8577c38a9ff820449e764f1ccd108039154f935a Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 14 Oct 2015 10:27:18 -0700 Subject: [PATCH] Show validation errors on save Fixes #17. --- app/pods/protected/characteristics/edit/controller.js | 5 +++-- app/pods/protected/characteristics/new/controller.js | 5 +++-- app/pods/protected/species/edit/controller.js | 5 +++-- app/pods/protected/species/new/controller.js | 5 +++-- app/pods/protected/strains/edit/controller.js | 5 +++-- app/pods/protected/strains/new/controller.js | 5 +++-- .../strains/show/measurements-table-row/component.js | 11 +++++++++-- app/pods/protected/strains/strain-form/component.js | 5 ----- app/pods/protected/users/edit/controller.js | 7 +++---- app/pods/users/new/controller.js | 9 +++------ app/utils/ajax-error.js | 7 +++++++ 11 files changed, 40 insertions(+), 29 deletions(-) create mode 100644 app/utils/ajax-error.js diff --git a/app/pods/protected/characteristics/edit/controller.js b/app/pods/protected/characteristics/edit/controller.js index d0f2ec0..c16ce3f 100644 --- a/app/pods/protected/characteristics/edit/controller.js +++ b/app/pods/protected/characteristics/edit/controller.js @@ -1,4 +1,5 @@ import Ember from 'ember'; +import ajaxError from '../../../../utils/ajax-error'; export default Ember.Controller.extend({ actions: { @@ -8,8 +9,8 @@ export default Ember.Controller.extend({ if (characteristic.get('hasDirtyAttributes')) { characteristic.save().then((characteristic) => { this.transitionToRoute('protected.characteristics.show', characteristic); - }, (err) => { - this.get('flashMessages').error(err.responseJSON.error); + }, () => { + ajaxError(characteristic.get('errors'), this.get('flashMessages')); }); } else { characteristic.deleteRecord(); diff --git a/app/pods/protected/characteristics/new/controller.js b/app/pods/protected/characteristics/new/controller.js index c211ec8..2b852e7 100644 --- a/app/pods/protected/characteristics/new/controller.js +++ b/app/pods/protected/characteristics/new/controller.js @@ -1,4 +1,5 @@ import Ember from 'ember'; +import ajaxError from '../../../../utils/ajax-error'; export default Ember.Controller.extend({ actions: { @@ -8,8 +9,8 @@ export default Ember.Controller.extend({ if (characteristic.get('hasDirtyAttributes')) { characteristic.save().then((characteristic) => { this.transitionToRoute('protected.characteristics.show', characteristic); - }, (err) => { - this.get('flashMessages').error(err.responseJSON.error); + }, () => { + ajaxError(characteristic.get('errors'), this.get('flashMessages')); }); } else { this.transitionToRoute('protected.characteristics.index'); diff --git a/app/pods/protected/species/edit/controller.js b/app/pods/protected/species/edit/controller.js index 429ff0b..186e7e4 100644 --- a/app/pods/protected/species/edit/controller.js +++ b/app/pods/protected/species/edit/controller.js @@ -1,4 +1,5 @@ import Ember from 'ember'; +import ajaxError from '../../../../utils/ajax-error'; export default Ember.Controller.extend({ actions: { @@ -8,8 +9,8 @@ export default Ember.Controller.extend({ if (species.get('hasDirtyAttributes')) { species.save().then((species) => { this.transitionToRoute('protected.species.show', species); - }, (err) => { - this.get('flashMessages').error(err.responseJSON.error); + }, () => { + ajaxError(species.get('errors'), this.get('flashMessages')); }); } else { this.transitionToRoute('protected.species.show', species); diff --git a/app/pods/protected/species/new/controller.js b/app/pods/protected/species/new/controller.js index cb5a59d..094ea14 100644 --- a/app/pods/protected/species/new/controller.js +++ b/app/pods/protected/species/new/controller.js @@ -1,4 +1,5 @@ import Ember from 'ember'; +import ajaxError from '../../../../utils/ajax-error'; export default Ember.Controller.extend({ actions: { @@ -8,8 +9,8 @@ export default Ember.Controller.extend({ if (species.get('hasDirtyAttributes')) { species.save().then((species) => { this.transitionToRoute('protected.species.show', species.get('id')); - }, (err) => { - this.get('flashMessages').error(err.responseJSON.error); + }, () => { + ajaxError(species.get('errors'), this.get('flashMessages')); }); } else { species.deleteRecord(); diff --git a/app/pods/protected/strains/edit/controller.js b/app/pods/protected/strains/edit/controller.js index d799574..b455768 100644 --- a/app/pods/protected/strains/edit/controller.js +++ b/app/pods/protected/strains/edit/controller.js @@ -1,4 +1,5 @@ import Ember from 'ember'; +import ajaxError from '../../../../utils/ajax-error'; export default Ember.Controller.extend({ actions: { @@ -8,8 +9,8 @@ export default Ember.Controller.extend({ if (strain.get('hasDirtyAttributes')) { strain.save().then((strain) => { this.transitionToRoute('protected.strains.show', strain); - }, (err) => { - this.get('flashMessages').error(err.responseJSON.error); + }, () => { + ajaxError(strain.get('errors'), this.get('flashMessages')); }); } else { strain.deleteRecord(); diff --git a/app/pods/protected/strains/new/controller.js b/app/pods/protected/strains/new/controller.js index 23fafed..0d186a4 100644 --- a/app/pods/protected/strains/new/controller.js +++ b/app/pods/protected/strains/new/controller.js @@ -1,4 +1,5 @@ import Ember from 'ember'; +import ajaxError from '../../../../utils/ajax-error'; export default Ember.Controller.extend({ actions: { @@ -8,8 +9,8 @@ export default Ember.Controller.extend({ if (strain.get('hasDirtyAttributes')) { strain.save().then((strain) => { this.transitionToRoute('protected.strains.show', strain); - }, (err) => { - this.get('flashMessages').error(err.responseJSON.error); + }, () => { + ajaxError(strain.get('errors'), this.get('flashMessages')); }); } else { this.transitionToRoute('protected.strains.index'); diff --git a/app/pods/protected/strains/show/measurements-table-row/component.js b/app/pods/protected/strains/show/measurements-table-row/component.js index a527572..d17a831 100644 --- a/app/pods/protected/strains/show/measurements-table-row/component.js +++ b/app/pods/protected/strains/show/measurements-table-row/component.js @@ -1,4 +1,5 @@ import Ember from 'ember'; +import ajaxError from '../../../../../utils/ajax-error'; export default Ember.Component.extend({ tagName: 'tr', @@ -22,9 +23,15 @@ export default Ember.Component.extend({ }, save: function() { - this.toggleProperty('isEditing'); if (this.get('rowChanged')) { - this.get('row').save(); + this.get('row').save().then(() => { + this.get('flashMessages').clearMessage(); + this.toggleProperty('isEditing'); + }, () => { + ajaxError(this.get('row.errors'), this.get('flashMessages')); + }); + } else { + this.toggleProperty('isEditing'); } }, diff --git a/app/pods/protected/strains/strain-form/component.js b/app/pods/protected/strains/strain-form/component.js index 0c96de3..a53a469 100644 --- a/app/pods/protected/strains/strain-form/component.js +++ b/app/pods/protected/strains/strain-form/component.js @@ -3,11 +3,6 @@ import Ember from 'ember'; export default Ember.Component.extend({ actions: { save: function() { - // Need to override the string id for some reason - // TODO: check this - let strain = this.get('strain'); - let id = strain.get('species.id'); - strain.set('species.id', +id); this.sendAction('save'); }, diff --git a/app/pods/protected/users/edit/controller.js b/app/pods/protected/users/edit/controller.js index 3d4940c..955c328 100644 --- a/app/pods/protected/users/edit/controller.js +++ b/app/pods/protected/users/edit/controller.js @@ -1,4 +1,5 @@ import Ember from 'ember'; +import ajaxError from '../../../../utils/ajax-error'; export default Ember.Controller.extend({ actions: { @@ -18,10 +19,8 @@ export default Ember.Controller.extend({ this.get('store').unloadAll(); } this.transitionToRoute('protected.users.show', user); - }, (err) => { - err.errors.forEach((error) => { - this.get('flashMessages').error(error.detail); - }); + }, () => { + ajaxError(user.get('errors'), this.get('flashMessages')); }); } else { this.transitionToRoute('protected.users.show', user); diff --git a/app/pods/users/new/controller.js b/app/pods/users/new/controller.js index fcc00d7..3285b1a 100644 --- a/app/pods/users/new/controller.js +++ b/app/pods/users/new/controller.js @@ -1,4 +1,5 @@ import Ember from 'ember'; +import ajaxError from '../../../utils/ajax-error'; export default Ember.Controller.extend({ passwordConfirm: null, @@ -20,12 +21,8 @@ export default Ember.Controller.extend({ this.get('flashMessages').information(`You have successfully signed up. Please check your email for further instructions.`); }); - }).catch(() => { - // Manually clean up messages because there is no transition - this.get('flashMessages').clearMessages(); - user.get('errors').forEach((error) => { - this.get('flashMessages').error(`${error.attribute.capitalize()} - ${error.message}`); - }); + }, () => { + ajaxError(user.get('errors'), this.get('flashMessages')); }); } }, diff --git a/app/utils/ajax-error.js b/app/utils/ajax-error.js new file mode 100644 index 0000000..76103f2 --- /dev/null +++ b/app/utils/ajax-error.js @@ -0,0 +1,7 @@ +export default function ajaxError(errors, flash) { + flash.clearMessages(); + errors.forEach((error) => { + console.log(error); + flash.error(`${error.attribute.replace(/([A-Z])/g, ' $1').capitalize()} - ${error.message}`); + }); +}