Ghost/ghost/admin/mixins/validation-engine.js
Jacob Gable ab97c8c861 Add validation for signin/signup
Closes #2976, Closes #3017

- Move logic to signup controller
  - Add ValidationEngine mixin to signup controller
  - Add signup validator with code from clientold login view
- Add signin validator and integrate into signin controller
- Add validation to forgotten controller
  - Switch to button action to support hitting enter in text field to submit
- Clear all notifications in notifications.closeAll
- Modify ValidationEngine.validate to not format errors based on option
- Update casper test for signin to wait for notification before trying
to do another signin
2014-06-23 21:17:57 -05:00

98 lines
3.2 KiB
JavaScript

import { getRequestErrorMessage } from 'ghost/utils/ajax';
import ValidatorExtensions from 'ghost/utils/validator-extensions';
import PostValidator from 'ghost/validators/post';
import SignupValidator from 'ghost/validators/signup';
import SigninValidator from 'ghost/validators/signin';
import ForgotValidator from 'ghost/validators/forgotten';
ValidatorExtensions.init();
var ValidationEngine = Ember.Mixin.create({
validators: {
post: PostValidator,
signup: SignupValidator,
signin: SigninValidator,
forgotten: ForgotValidator
},
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(' ');
} 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;