Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import DS from 'ember-data';
- import {buildValidations, validator} from 'ember-cp-validations';
- const Validations = buildValidations({
- username: validator('custom-presence', {
- message: 'username: should not be empty'
- }),
- password: [
- validator('length', {
- min: 4,
- message: 'Password: must be 4+ long'
- }),
- validator('custom-presence', {
- presence: true,
- message: 'Password: should not be empty or consist only of spaces'
- })]
- });
- export default DS.Model.extend(Validations, {
- username: DS.attr('string'),
- password: DS.attr('string')
- });
- ==============================
- import BaseValidator from 'ember-cp-validations/validators/base';
- const CustomPresence = BaseValidator.extend({
- validate(value, options, model, attribute) {
- const trimmed = value.trim();
- if (trimmed.length === 0) {
- return false;
- }
- return true;
- }
- });
- export default CustomPresence;
- ==================================
- /* eslint-disable import/extensions,no-shadow,max-len */
- import Ember from 'ember';
- import _ from 'lodash';
- export default Ember.Controller.extend({
- session: Ember.inject.service('session'),
- isDisabled: Ember.computed('model', 'confirmPassword', function () {
- const username = this.get('model.username');
- const password = this.get('model.password');
- const confirmPassword = this.get('confirmPassword');
- return !(username && password && confirmPassword);
- }),
- passwordsNotPresented(password, passwordConfirmation) {
- return _.isUndefined(password) || _.isUndefined(passwordConfirmation);
- },
- passwordsNotEqual(password, passwordConfirmation) {
- return !(_.isEqual(password, passwordConfirmation));
- },
- passwordsNotPresentedHandler() {
- this.set('resultComponent', 'error-panel');
- this.set('messages', 'Input some passwords first');
- },
- passwordsNotEqualHandler() {
- this.set('resultComponent', 'error-panel');
- this.set('messages', 'Passwords are unequal');
- },
- allStepsPassedHandler() {
- this.get('model')
- .validate()
- // eslint-disable-next-line consistent-return
- .then(({validations}) => {
- if (validations.get('isValid')) {
- this.get('model')
- .save()
- .then(() => {
- this.transitionToRoute('admins');
- })
- .catch(error => {
- this.set('resultComponent', 'error-panel');
- this.set('messages', error);
- });
- } else {
- this.set('resultComponent', 'error-panel');
- this.set('messages', validations.get('messages'));
- }
- })
- .catch(error => {
- this.set('resultComponent', 'error-panel');
- this.set('messages', error);
- });
- },
- userNameNotPresented() {
- return this.get('model.username').trim().length === 0;
- },
- userNameNotPresentedHandler() {
- this.set('resultComponent', 'error-panel');
- this.set('messages', 'Username could not be empty');
- },
- checkIt() {
- const exactChecking = _.cond([
- // [
- // (password, passwordConfirmation) => this.passwordsNotPresented(password, passwordConfirmation),
- // (password, passwordConfirmation) => this.passwordsNotPresentedHandler(password, passwordConfirmation)
- // ],
- [
- (password, passwordConfirmation) => this.passwordsNotEqual(password, passwordConfirmation),
- (password, passwordConfirmation) => this.passwordsNotEqualHandler(password, passwordConfirmation)
- ],
- // [
- // () => this.userNameNotPresented(),
- // () => this.userNameNotPresentedHandler()
- // ],
- [
- () => true,
- () => this.allStepsPassedHandler()
- ]
- ]);
- const password = this.get('model.password');
- const passwordConfirmation = this.get('confirmPassword');
- exactChecking(password, passwordConfirmation);
- },
- actions: {
- signUp() {
- this.checkIt();
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement