2014-06-21 01:36:44 +04:00
|
|
|
import { getRequestErrorMessage } from 'ghost/utils/ajax';
|
|
|
|
|
|
|
|
import ValidatorExtensions from 'ghost/utils/validator-extensions';
|
|
|
|
import PostValidator from 'ghost/validators/post';
|
2014-06-25 16:12:48 +04:00
|
|
|
import SetupValidator from 'ghost/validators/setup';
|
2014-06-24 04:47:51 +04:00
|
|
|
import SignupValidator from 'ghost/validators/signup';
|
|
|
|
import SigninValidator from 'ghost/validators/signin';
|
|
|
|
import ForgotValidator from 'ghost/validators/forgotten';
|
2014-06-24 10:33:24 +04:00
|
|
|
import SettingValidator from 'ghost/validators/setting';
|
2014-06-21 01:36:44 +04:00
|
|
|
|
|
|
|
ValidatorExtensions.init();
|
|
|
|
|
|
|
|
var ValidationEngine = Ember.Mixin.create({
|
|
|
|
validators: {
|
2014-06-24 04:47:51 +04:00
|
|
|
post: PostValidator,
|
2014-06-25 16:12:48 +04:00
|
|
|
setup: SetupValidator,
|
2014-06-24 04:47:51 +04:00
|
|
|
signup: SignupValidator,
|
|
|
|
signin: SigninValidator,
|
2014-06-24 10:33:24 +04:00
|
|
|
forgotten: ForgotValidator,
|
|
|
|
setting: SettingValidator
|
2014-06-21 01:36:44 +04:00
|
|
|
},
|
|
|
|
|
2014-06-24 04:47:51 +04:00
|
|
|
validate: function (opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
2014-06-21 01:36:44 +04:00
|
|
|
var self = this,
|
|
|
|
type = this.get('validationType'),
|
|
|
|
validator = this.get('validators.' + type);
|
|
|
|
|
|
|
|
return new Ember.RSVP.Promise(function (resolve, reject) {
|
|
|
|
if (!type || !validator) {
|
2014-06-22 01:59:12 +04:00
|
|
|
return reject(self.formatErrors('The validator specified, "' + type + '", did not exist!'));
|
2014-06-21 01:36:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var validationErrors = validator.validate(self);
|
|
|
|
|
|
|
|
if (Ember.isEmpty(validationErrors)) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
|
2014-06-24 04:47:51 +04:00
|
|
|
if (opts.format !== false) {
|
|
|
|
validationErrors = self.formatErrors(validationErrors);
|
|
|
|
}
|
|
|
|
|
|
|
|
return reject(validationErrors);
|
2014-06-21 01:36:44 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-06-22 01:59:12 +04:00
|
|
|
// format errors to be used in `notifications.showErrors`.
|
|
|
|
// format is [{ message: 'concatenated error messages' }]
|
|
|
|
formatErrors: function (errors) {
|
|
|
|
var message = 'There was an error saving this ' + this.get('validationType');
|
|
|
|
|
|
|
|
if (Ember.isArray(errors)) {
|
|
|
|
// get validation error messages
|
2014-06-26 13:42:29 +04:00
|
|
|
message = errors.mapBy('message').join('<br />');
|
2014-06-22 01:59:12 +04:00
|
|
|
} else if (typeof errors === 'object') {
|
|
|
|
// Get messages from server response
|
|
|
|
message += ': ' + getRequestErrorMessage(errors);
|
|
|
|
} else if (typeof errors === 'string') {
|
|
|
|
message += ': ' + errors;
|
|
|
|
} else {
|
|
|
|
message += '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
// set format for notifications.showErrors
|
|
|
|
message = [{ message: message }];
|
|
|
|
|
|
|
|
return message;
|
|
|
|
},
|
|
|
|
|
2014-06-21 01:36:44 +04:00
|
|
|
// override save to do validation first
|
|
|
|
save: function () {
|
|
|
|
var self = this,
|
|
|
|
// this is a hack, but needed for async _super calls.
|
|
|
|
// ref: https://github.com/emberjs/ember.js/pull/4301
|
|
|
|
_super = this.__nextSuper;
|
|
|
|
|
2014-06-22 01:59:12 +04:00
|
|
|
// model.destroyRecord() calls model.save() behind the scenes.
|
|
|
|
// in that case, we don't need validation checks or error propagation.
|
|
|
|
if (this.get('isDeleted')) {
|
|
|
|
return this._super();
|
|
|
|
}
|
|
|
|
|
2014-06-21 01:36:44 +04:00
|
|
|
// If validation fails, reject with validation errors.
|
|
|
|
// If save to the server fails, reject with server response.
|
|
|
|
return this.validate().then(function () {
|
|
|
|
return _super.call(self);
|
|
|
|
}).catch(function (result) {
|
2014-06-22 01:59:12 +04:00
|
|
|
// server save failed, format the errors and reject the promise.
|
|
|
|
// if validations failed, the errors will already be formatted for us.
|
|
|
|
if (! Ember.isArray(result)) {
|
|
|
|
result = self.formatErrors(result);
|
2014-06-21 01:36:44 +04:00
|
|
|
}
|
|
|
|
|
2014-06-22 01:59:12 +04:00
|
|
|
return Ember.RSVP.reject(result);
|
2014-06-21 01:36:44 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default ValidationEngine;
|