From 5db2d0907090dd88512066ae0e0fe338fe1830a5 Mon Sep 17 00:00:00 2001 From: vogdb Date: Tue, 6 Dec 2016 21:28:30 +0300 Subject: [PATCH 1/6] Handle Required errors as separate Exception --- library/CM/Exception/FormFieldRequired.js | 16 ++++++++++++++++ library/CM/Form/Abstract.js | 9 ++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 library/CM/Exception/FormFieldRequired.js diff --git a/library/CM/Exception/FormFieldRequired.js b/library/CM/Exception/FormFieldRequired.js new file mode 100644 index 000000000..3f95748c6 --- /dev/null +++ b/library/CM/Exception/FormFieldRequired.js @@ -0,0 +1,16 @@ +(function(global) { + + /** + * @class CM_Exception_FormFieldRequired + * @extends CM_Exception_FormFieldValidation + */ + function CM_Exception_FormFieldRequired() { + global.CM_Exception_FormFieldValidation.apply(this, arguments); + this.name = 'CM_Exception_FormFieldRequired'; + } + + CM_Exception_FormFieldRequired.prototype = Object.create(global.CM_Exception_FormFieldValidation.prototype); + CM_Exception_FormFieldRequired.prototype.constructor = CM_Exception_FormFieldRequired; + + global.CM_Exception_FormFieldRequired = CM_Exception_FormFieldRequired; +})(window); \ No newline at end of file diff --git a/library/CM/Form/Abstract.js b/library/CM/Form/Abstract.js index 6c945d15a..eb9bd7c71 100755 --- a/library/CM/Form/Abstract.js +++ b/library/CM/Form/Abstract.js @@ -206,7 +206,7 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ this.resetErrors(); if (_.size(errorList)) { - var error = new CM_Exception_FormFieldValidation(); + var error = new CM_Exception_FormFieldRequired(); error.setErrorList(errorList); return Promise.reject(error); } @@ -256,6 +256,13 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ if (displayErrors) { this._displayValidationError(error); } + var isRequired = error instanceof CM_Exception_FormFieldRequired; + if (isRequired) { + if (displayErrors) { + return; + } + throw error; + } this._stopErrorPropagation = false; this.trigger('error error.' + actionName, error.message, error.name, error.isPublic); if (!this._stopErrorPropagation && !displayErrors) { From 1fc7f4e77885f87899b16f3739c2b9cbc908c063 Mon Sep 17 00:00:00 2001 From: vogdb Date: Wed, 7 Dec 2016 16:54:05 +0300 Subject: [PATCH 2/6] rename CM_Form_Abstract._getErrorList to _getErrorListRequired --- library/CM/Form/Abstract.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/CM/Form/Abstract.js b/library/CM/Form/Abstract.js index eb9bd7c71..171ea3d0e 100755 --- a/library/CM/Form/Abstract.js +++ b/library/CM/Form/Abstract.js @@ -202,12 +202,12 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ _submitOnly: function(actionName, disableUI) { var action = this._getAction(actionName); var data = this.getActionData(action.name); - var errorList = this._getErrorList(action.name, data); + var errorListRequired = this._getErrorListRequired(action.name, data); this.resetErrors(); - if (_.size(errorList)) { + if (_.size(errorListRequired)) { var error = new CM_Exception_FormFieldRequired(); - error.setErrorList(errorList); + error.setErrorList(errorListRequired); return Promise.reject(error); } @@ -338,7 +338,7 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ * @param {Object} data * @returns {Array[]} */ - _getErrorList: function(actionName, data) { + _getErrorListRequired: function(actionName, data) { var action = this._getAction(actionName); var errorList = []; From 9cb7c5e907a2aaff9e3e2a2ee489755032726ab2 Mon Sep 17 00:00:00 2001 From: vogdb Date: Wed, 7 Dec 2016 19:43:02 +0300 Subject: [PATCH 3/6] Introduce CM_Form_Abstract._handleSubmitError and separation of error handling --- library/CM/Form/Abstract.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/library/CM/Form/Abstract.js b/library/CM/Form/Abstract.js index 171ea3d0e..72b4c46cb 100755 --- a/library/CM/Form/Abstract.js +++ b/library/CM/Form/Abstract.js @@ -59,8 +59,8 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ .then(function() { field.success(); }) - .catch(CM_Exception_FormFieldValidation, function(error) { - handler._handleValidationError(error, handler.autosave, true); + .catch(function(error) { + handler._handleSubmitError(error, handler.autosave, true); }); } }); @@ -189,8 +189,8 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ .try(function() { return this._submitOnly(actionName, options.disableUI); }) - .catch(CM_Exception_FormFieldValidation, function(error) { - this._handleValidationError(error, actionName, options.handleErrors); + .catch(function(error) { + this._handleSubmitError(error, actionName, options.handleErrors); }); }, @@ -247,6 +247,23 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ }); }, + _handleSubmitError: function(error, actionName, displayErrors) { + if (error instanceof CM_Exception_FormFieldRequired) { + this._handleRequiredError(error, actionName, displayErrors); + } else if (error instanceof CM_Exception_FormFieldValidation) { + this._handleValidationError(error, actionName, displayErrors); + } else { + throw error; + } + }, + + _handleRequiredError: function(error, actionName, displayErrors) { + if (displayErrors) { + return this._displayValidationError(error); + } + throw error; + }, + /** * @param {CM_Exception_FormFieldValidation} error * @param {String} actionName @@ -256,13 +273,6 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ if (displayErrors) { this._displayValidationError(error); } - var isRequired = error instanceof CM_Exception_FormFieldRequired; - if (isRequired) { - if (displayErrors) { - return; - } - throw error; - } this._stopErrorPropagation = false; this.trigger('error error.' + actionName, error.message, error.name, error.isPublic); if (!this._stopErrorPropagation && !displayErrors) { From 405bab2546b71f70c13595fbf9745acbe2a27a2b Mon Sep 17 00:00:00 2001 From: vogdb Date: Thu, 8 Dec 2016 10:26:22 +0300 Subject: [PATCH 4/6] Remove obsolete logic from CM_Form_Abstract --- library/CM/Exception/FormFieldRequired.js | 16 ----- library/CM/Form/Abstract.js | 72 +++-------------------- 2 files changed, 9 insertions(+), 79 deletions(-) delete mode 100644 library/CM/Exception/FormFieldRequired.js diff --git a/library/CM/Exception/FormFieldRequired.js b/library/CM/Exception/FormFieldRequired.js deleted file mode 100644 index 3f95748c6..000000000 --- a/library/CM/Exception/FormFieldRequired.js +++ /dev/null @@ -1,16 +0,0 @@ -(function(global) { - - /** - * @class CM_Exception_FormFieldRequired - * @extends CM_Exception_FormFieldValidation - */ - function CM_Exception_FormFieldRequired() { - global.CM_Exception_FormFieldValidation.apply(this, arguments); - this.name = 'CM_Exception_FormFieldRequired'; - } - - CM_Exception_FormFieldRequired.prototype = Object.create(global.CM_Exception_FormFieldValidation.prototype); - CM_Exception_FormFieldRequired.prototype.constructor = CM_Exception_FormFieldRequired; - - global.CM_Exception_FormFieldRequired = CM_Exception_FormFieldRequired; -})(window); \ No newline at end of file diff --git a/library/CM/Form/Abstract.js b/library/CM/Form/Abstract.js index 72b4c46cb..fd602b135 100755 --- a/library/CM/Form/Abstract.js +++ b/library/CM/Form/Abstract.js @@ -14,9 +14,6 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ /** @type Object **/ _actions: {}, - /** @type Boolean **/ - _stopErrorPropagation: false, - initialize: function() { CM_View_Abstract.prototype.initialize.call(this); @@ -59,8 +56,8 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ .then(function() { field.success(); }) - .catch(function(error) { - handler._handleSubmitError(error, handler.autosave, true); + .catch(CM_Exception_FormFieldValidation, function(error) { + this._displayValidationError(error); }); } }); @@ -176,46 +173,36 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ /** * @param {String} [actionName] - * @param {Object} [options] * @return Promise */ - submit: function(actionName, options) { - options = _.defaults(options || {}, { - handleErrors: true, - disableUI: true - }); - + submit: function(actionName) { return this .try(function() { - return this._submitOnly(actionName, options.disableUI); + return this._submitOnly(actionName); }) - .catch(function(error) { - this._handleSubmitError(error, actionName, options.handleErrors); + .catch(CM_Exception_FormFieldValidation, function(error) { + this._displayValidationError(error); }); }, /** * @param {String} [actionName] - * @param {Boolean} [disableUI] * @return Promise */ - _submitOnly: function(actionName, disableUI) { + _submitOnly: function(actionName) { var action = this._getAction(actionName); var data = this.getActionData(action.name); var errorListRequired = this._getErrorListRequired(action.name, data); this.resetErrors(); if (_.size(errorListRequired)) { - var error = new CM_Exception_FormFieldRequired(); + var error = new CM_Exception_FormFieldValidation(); error.setErrorList(errorListRequired); return Promise.reject(error); } return this .try(function() { - if (disableUI) { - this.disable(); - } this.trigger('submit', [data]); return cm.ajax('form', {viewInfoList: this.getViewInfoList(), actionName: action.name, data: data}) }) @@ -223,6 +210,7 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ if (response.errors) { var error = new CM_Exception_FormFieldValidation(); error.setErrorList(response.errors); + this.trigger('error error.' + actionName, error.message, error.name, error.isPublic); throw error; } @@ -239,47 +227,9 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ this.trigger('success success.' + action.name, response.data); return response.data; - }) - .finally(function() { - if (disableUI) { - this.enable(); - } }); }, - _handleSubmitError: function(error, actionName, displayErrors) { - if (error instanceof CM_Exception_FormFieldRequired) { - this._handleRequiredError(error, actionName, displayErrors); - } else if (error instanceof CM_Exception_FormFieldValidation) { - this._handleValidationError(error, actionName, displayErrors); - } else { - throw error; - } - }, - - _handleRequiredError: function(error, actionName, displayErrors) { - if (displayErrors) { - return this._displayValidationError(error); - } - throw error; - }, - - /** - * @param {CM_Exception_FormFieldValidation} error - * @param {String} actionName - * @param {Boolean} displayErrors - */ - _handleValidationError: function(error, actionName, displayErrors) { - if (displayErrors) { - this._displayValidationError(error); - } - this._stopErrorPropagation = false; - this.trigger('error error.' + actionName, error.message, error.name, error.isPublic); - if (!this._stopErrorPropagation && !displayErrors) { - throw error; - } - }, - /** * @param {CM_Exception_FormFieldValidation} validationError */ @@ -294,10 +244,6 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ } }, - stopErrorPropagation: function() { - this._stopErrorPropagation = true; - }, - reset: function() { this.el.reset(); }, From f28134b0600524c07b9146a24ff85ce68b783f5f Mon Sep 17 00:00:00 2001 From: vogdb Date: Thu, 8 Dec 2016 14:52:24 +0300 Subject: [PATCH 5/6] Streamline removal of obsolete logic from CM_Form_Abstract --- library/CM/Form/Abstract.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/library/CM/Form/Abstract.js b/library/CM/Form/Abstract.js index fd602b135..a60e15409 100755 --- a/library/CM/Form/Abstract.js +++ b/library/CM/Form/Abstract.js @@ -172,13 +172,13 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ }, /** - * @param {String} [actionName] + * @param {String} actionName * @return Promise */ submit: function(actionName) { return this .try(function() { - return this._submitOnly(actionName); + return this._submitOnly(actionName, true); }) .catch(CM_Exception_FormFieldValidation, function(error) { this._displayValidationError(error); @@ -186,10 +186,11 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ }, /** - * @param {String} [actionName] + * @param {String} actionName + * @param {Boolean} [disableUI] * @return Promise */ - _submitOnly: function(actionName) { + _submitOnly: function(actionName, disableUI) { var action = this._getAction(actionName); var data = this.getActionData(action.name); var errorListRequired = this._getErrorListRequired(action.name, data); @@ -203,6 +204,9 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ return this .try(function() { + if (disableUI) { + this.disable(); + } this.trigger('submit', [data]); return cm.ajax('form', {viewInfoList: this.getViewInfoList(), actionName: action.name, data: data}) }) @@ -210,7 +214,7 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ if (response.errors) { var error = new CM_Exception_FormFieldValidation(); error.setErrorList(response.errors); - this.trigger('error error.' + actionName, error.message, error.name, error.isPublic); + this.trigger('error error.' + actionName, error); throw error; } @@ -227,6 +231,11 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ this.trigger('success success.' + action.name, response.data); return response.data; + }) + .finally(function() { + if (disableUI) { + this.enable(); + } }); }, From e98d6f7b1f10ce4c2fc2b82e00f7f61fb553ec51 Mon Sep 17 00:00:00 2001 From: vogdb Date: Thu, 8 Dec 2016 15:08:23 +0300 Subject: [PATCH 6/6] Docu --- library/CM/Form/Abstract.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/CM/Form/Abstract.js b/library/CM/Form/Abstract.js index a60e15409..ef5f4b760 100755 --- a/library/CM/Form/Abstract.js +++ b/library/CM/Form/Abstract.js @@ -187,7 +187,7 @@ var CM_Form_Abstract = CM_View_Abstract.extend({ /** * @param {String} actionName - * @param {Boolean} [disableUI] + * @param {Boolean} disableUI * @return Promise */ _submitOnly: function(actionName, disableUI) {