Merge pull request #3248 from jaswilli/issue-3246

Add validation to invite new user form.
This commit is contained in:
Hannah Wolfe 2014-07-13 21:13:33 +01:00
commit 41fcf2661c
4 changed files with 79 additions and 39 deletions

View File

@ -24,18 +24,22 @@ var InviteNewUserController = Ember.Controller.extend({
self = this,
newUser;
this.notifications.closePassive();
newUser = this.store.createRecord('user', {
'email': email,
'role': role_id
email: email,
role: role_id,
status: 'invited'
});
newUser.save().then(function () {
var notificationText = 'Invitation sent! (' + email + ')';
self.notifications.showSuccess(notificationText, false);
}).fail(function (error) {
}).catch(function (errors) {
newUser.deleteRecord();
self.notifications.closePassive();
self.notifications.showAPIError(error);
self.notifications.showErrors(errors);
});
this.set('email', null);

View File

@ -8,6 +8,7 @@ import SigninValidator from 'ghost/validators/signin';
import ForgotValidator from 'ghost/validators/forgotten';
import SettingValidator from 'ghost/validators/setting';
import ResetValidator from 'ghost/validators/reset';
import UserValidator from 'ghost/validators/user';
// our extensions to the validator library
ValidatorExtensions.init();
@ -70,7 +71,8 @@ var ValidationEngine = Ember.Mixin.create({
signin: SigninValidator,
forgotten: ForgotValidator,
setting: SettingValidator,
reset: ResetValidator
reset: ResetValidator,
user: UserValidator
},
/**

View File

@ -1,4 +1,8 @@
var User = DS.Model.extend({
import ValidationEngine from 'ghost/mixins/validation-engine';
var User = DS.Model.extend(ValidationEngine, {
validationType: 'user',
uuid: DS.attr('string'),
name: DS.attr('string'),
slug: DS.attr('string'),
@ -20,35 +24,6 @@ var User = DS.Model.extend({
updated_at: DS.attr('moment-date'),
updated_by: DS.attr('number'),
validationErrors: function () {
var validationErrors = [];
if (!validator.isLength(this.get('name'), 0, 150)) {
validationErrors.push({message: 'Name is too long'});
}
if (!validator.isLength(this.get('bio'), 0, 200)) {
validationErrors.push({message: 'Bio is too long'});
}
if (!validator.isEmail(this.get('email'))) {
validationErrors.push({message: 'Please supply a valid email address'});
}
if (!validator.isLength(this.get('location'), 0, 150)) {
validationErrors.push({message: 'Location is too long'});
}
if (!validator.isURL(this.get('website'), { protocols: ['http', 'https'], require_protocol: true }) ||
!validator.isLength(this.get('website'), 0, 2000)) {
validationErrors.push({message: 'Please use a valid url'});
}
return validationErrors;
}.property('name', 'bio', 'email', 'location', 'website'),
isValid: Ember.computed.empty('validationErrors.[]'),
saveNewPassword: function (password) {
var url = this.get('ghostPaths').adminUrl('changepw');
return ic.ajax.request(url, {
@ -81,10 +56,7 @@ var User = DS.Model.extend({
}
return validationErrors;
},
}
});

View File

@ -0,0 +1,62 @@
var UserValidator = Ember.Object.create({
check: function (model) {
var validator = this.validators[model.get('status')];
if (typeof validator !== 'function') {
return [];
}
return validator(model);
},
validators: {
invited: function (model) {
var validationErrors = [],
email = model.get('email'),
role = model.get('role');
if (!validator.isEmail(email)) {
validationErrors.push({ message: 'Please supply a valid email address' });
}
if (!validator.isLength(role, 1)) {
validationErrors.push({ message: 'Please select a role' });
}
return validationErrors;
},
active: function (model) {
var validationErrors = [],
name = model.get('name'),
bio = model.get('bio'),
email = model.get('email'),
location = model.get('location'),
website = model.get('website');
if (!validator.isLength(name, 0, 150)) {
validationErrors.push({ message: 'Name is too long' });
}
if (!validator.isLength(bio, 0, 200)) {
validationErrors.push({ message: 'Bio is too long' });
}
if (!validator.isEmail(email)) {
validationErrors.push({ message: 'Please supply a valid email address' });
}
if (!validator.isLength(location, 0, 150)) {
validationErrors.push({ message: 'Location is too long' });
}
if (!validator.isURL(website, { protocols: ['http', 'https'], require_protocol: true }) ||
!validator.isLength(website, 0, 2000)) {
validationErrors.push({ message: 'Please use a valid url' });
}
}
}
});
export default UserValidator;