2015-06-07 06:19:19 +03:00
|
|
|
import BaseValidator from './base';
|
2016-06-14 14:46:24 +03:00
|
|
|
import {isBlank} from 'ember-utils';
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-08-19 14:55:40 +03:00
|
|
|
export default BaseValidator.create({
|
2015-06-07 06:19:19 +03:00
|
|
|
properties: ['name', 'bio', 'email', 'location', 'website', 'roles'],
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
isActive(model) {
|
2015-06-07 06:19:19 +03:00
|
|
|
return (model.get('status') === 'active');
|
2014-07-11 23:01:42 +04:00
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
name(model) {
|
|
|
|
let name = model.get('name');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2015-07-07 20:14:23 +03:00
|
|
|
if (validator.empty(name)) {
|
|
|
|
model.get('errors').add('name', 'Please enter a name.');
|
|
|
|
this.invalidate();
|
|
|
|
} else if (!validator.isLength(name, 0, 150)) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('name', 'Name is too long');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
bio(model) {
|
|
|
|
let bio = model.get('bio');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2014-07-11 23:01:42 +04:00
|
|
|
if (!validator.isLength(bio, 0, 200)) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('bio', 'Bio is too long');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
email(model) {
|
|
|
|
let email = model.get('email');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (!validator.isEmail(email)) {
|
|
|
|
model.get('errors').add('email', 'Please supply a valid email address');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
location(model) {
|
|
|
|
let location = model.get('location');
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2014-07-11 23:01:42 +04:00
|
|
|
if (!validator.isLength(location, 0, 150)) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('location', 'Location is too long');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
2014-07-11 23:01:42 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
website(model) {
|
|
|
|
let website = model.get('website');
|
2016-11-14 16:16:51 +03:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
let isInvalidWebsite = !validator.isURL(website, {require_protocol: false})
|
|
|
|
|| !validator.isLength(website, 0, 2000);
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (this.isActive(model)) {
|
2016-11-14 16:16:51 +03:00
|
|
|
if (!validator.empty(website) && isInvalidWebsite) {
|
2015-06-07 06:19:19 +03:00
|
|
|
model.get('errors').add('website', 'Website is not a valid url');
|
|
|
|
this.invalidate();
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2015-06-07 06:19:19 +03:00
|
|
|
}
|
|
|
|
},
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
roles(model) {
|
2015-06-07 06:19:19 +03:00
|
|
|
if (!this.isActive(model)) {
|
2015-10-28 14:36:45 +03:00
|
|
|
let roles = model.get('roles');
|
2014-07-14 20:32:55 +04:00
|
|
|
|
2015-06-07 06:19:19 +03:00
|
|
|
if (roles.length < 1) {
|
|
|
|
model.get('errors').add('role', 'Please select a role');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
2016-06-14 14:46:24 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
passwordChange(model) {
|
|
|
|
let newPassword = model.get('newPassword');
|
|
|
|
let ne2Password = model.get('ne2Password');
|
|
|
|
|
|
|
|
// validation only marks the requested property as validated so we
|
|
|
|
// have to add properties manually
|
|
|
|
model.get('hasValidated').addObject('newPassword');
|
|
|
|
model.get('hasValidated').addObject('ne2Password');
|
|
|
|
|
|
|
|
if (isBlank(newPassword) && isBlank(ne2Password)) {
|
|
|
|
model.get('errors').add('newPassword', 'Sorry, passwords can\'t be blank');
|
|
|
|
this.invalidate();
|
|
|
|
} else {
|
|
|
|
if (!validator.equals(newPassword, ne2Password)) {
|
|
|
|
model.get('errors').add('ne2Password', 'Your new passwords do not match');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validator.isLength(newPassword, 8)) {
|
|
|
|
model.get('errors').add('newPassword', 'Your password must be at least 8 characters long.');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ownPasswordChange(model) {
|
|
|
|
let oldPassword = model.get('password');
|
|
|
|
|
|
|
|
this.passwordChange(model);
|
|
|
|
|
|
|
|
// validation only marks the requested property as validated so we
|
|
|
|
// have to add properties manually
|
|
|
|
model.get('hasValidated').addObject('password');
|
|
|
|
|
|
|
|
if (isBlank(oldPassword)) {
|
|
|
|
model.get('errors').add('password', 'Your current password is required to set a new one');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
2014-07-11 23:01:42 +04:00
|
|
|
}
|
|
|
|
});
|