mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-02 08:13:34 +03:00
3a7be0c2fd
Closes #2843 * Implemnted the ember validator correctly for both reset request and actual reset (with the token) * added reset validator * changed the request route addresses to be `/authentication/passwordreset` * changed the format of data to be `{ thing: [ {data } ] }` Missing: * notifications * tests for these use cases
104 lines
3.5 KiB
JavaScript
104 lines
3.5 KiB
JavaScript
import { getRequestErrorMessage } from 'ghost/utils/ajax';
|
|
|
|
import ValidatorExtensions from 'ghost/utils/validator-extensions';
|
|
import PostValidator from 'ghost/validators/post';
|
|
import SetupValidator from 'ghost/validators/setup';
|
|
import SignupValidator from 'ghost/validators/signup';
|
|
import SigninValidator from 'ghost/validators/signin';
|
|
import ForgotValidator from 'ghost/validators/forgotten';
|
|
import SettingValidator from 'ghost/validators/setting';
|
|
import ResetValidator from 'ghost/validators/reset';
|
|
|
|
ValidatorExtensions.init();
|
|
|
|
var ValidationEngine = Ember.Mixin.create({
|
|
validators: {
|
|
post: PostValidator,
|
|
setup: SetupValidator,
|
|
signup: SignupValidator,
|
|
signin: SigninValidator,
|
|
forgotten: ForgotValidator,
|
|
setting: SettingValidator,
|
|
reset: ResetValidator
|
|
},
|
|
|
|
validate: function (opts) {
|
|
opts = opts || {};
|
|
|
|
var self = this,
|
|
type = this.get('validationType'),
|
|
validator = this.get('validators.' + type);
|
|
|
|
return new Ember.RSVP.Promise(function (resolve, reject) {
|
|
if (!type || !validator) {
|
|
return reject(self.formatErrors('The validator specified, "' + type + '", did not exist!'));
|
|
}
|
|
|
|
var validationErrors = validator.validate(self);
|
|
|
|
if (Ember.isEmpty(validationErrors)) {
|
|
return resolve();
|
|
}
|
|
|
|
if (opts.format !== false) {
|
|
validationErrors = self.formatErrors(validationErrors);
|
|
}
|
|
|
|
return reject(validationErrors);
|
|
});
|
|
},
|
|
|
|
// 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
|
|
message = errors.mapBy('message').join('<br />');
|
|
} 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;
|
|
},
|
|
|
|
// 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;
|
|
|
|
// 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();
|
|
}
|
|
|
|
// 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) {
|
|
// 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);
|
|
}
|
|
|
|
return Ember.RSVP.reject(result);
|
|
});
|
|
}
|
|
});
|
|
|
|
export default ValidationEngine;
|