Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. import DS from 'ember-data';
  2. import {buildValidations, validator} from 'ember-cp-validations';
  3.  
  4. const Validations = buildValidations({
  5. username: validator('custom-presence', {
  6. message: 'username: should not be empty'
  7. }),
  8. password: [
  9. validator('length', {
  10. min: 4,
  11. message: 'Password: must be 4+ long'
  12. }),
  13. validator('custom-presence', {
  14. presence: true,
  15. message: 'Password: should not be empty or consist only of spaces'
  16. })]
  17. });
  18.  
  19. export default DS.Model.extend(Validations, {
  20. username: DS.attr('string'),
  21. password: DS.attr('string')
  22. });
  23.  
  24.  
  25. ==============================
  26. import BaseValidator from 'ember-cp-validations/validators/base';
  27.  
  28. const CustomPresence = BaseValidator.extend({
  29. validate(value, options, model, attribute) {
  30. const trimmed = value.trim();
  31. if (trimmed.length === 0) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. });
  37.  
  38.  
  39. export default CustomPresence;
  40. ==================================
  41. /* eslint-disable import/extensions,no-shadow,max-len */
  42. import Ember from 'ember';
  43. import _ from 'lodash';
  44.  
  45.  
  46. export default Ember.Controller.extend({
  47. session: Ember.inject.service('session'),
  48. isDisabled: Ember.computed('model', 'confirmPassword', function () {
  49. const username = this.get('model.username');
  50. const password = this.get('model.password');
  51. const confirmPassword = this.get('confirmPassword');
  52. return !(username && password && confirmPassword);
  53. }),
  54. passwordsNotPresented(password, passwordConfirmation) {
  55. return _.isUndefined(password) || _.isUndefined(passwordConfirmation);
  56. },
  57.  
  58. passwordsNotEqual(password, passwordConfirmation) {
  59. return !(_.isEqual(password, passwordConfirmation));
  60. },
  61.  
  62. passwordsNotPresentedHandler() {
  63. this.set('resultComponent', 'error-panel');
  64. this.set('messages', 'Input some passwords first');
  65. },
  66.  
  67. passwordsNotEqualHandler() {
  68. this.set('resultComponent', 'error-panel');
  69. this.set('messages', 'Passwords are unequal');
  70. },
  71. allStepsPassedHandler() {
  72. this.get('model')
  73. .validate()
  74. // eslint-disable-next-line consistent-return
  75. .then(({validations}) => {
  76. if (validations.get('isValid')) {
  77. this.get('model')
  78. .save()
  79. .then(() => {
  80. this.transitionToRoute('admins');
  81. })
  82. .catch(error => {
  83. this.set('resultComponent', 'error-panel');
  84. this.set('messages', error);
  85. });
  86. } else {
  87. this.set('resultComponent', 'error-panel');
  88. this.set('messages', validations.get('messages'));
  89. }
  90. })
  91. .catch(error => {
  92. this.set('resultComponent', 'error-panel');
  93. this.set('messages', error);
  94. });
  95. },
  96. userNameNotPresented() {
  97. return this.get('model.username').trim().length === 0;
  98. },
  99. userNameNotPresentedHandler() {
  100. this.set('resultComponent', 'error-panel');
  101. this.set('messages', 'Username could not be empty');
  102. },
  103. checkIt() {
  104. const exactChecking = _.cond([
  105. // [
  106. // (password, passwordConfirmation) => this.passwordsNotPresented(password, passwordConfirmation),
  107. // (password, passwordConfirmation) => this.passwordsNotPresentedHandler(password, passwordConfirmation)
  108. // ],
  109. [
  110. (password, passwordConfirmation) => this.passwordsNotEqual(password, passwordConfirmation),
  111. (password, passwordConfirmation) => this.passwordsNotEqualHandler(password, passwordConfirmation)
  112. ],
  113. // [
  114. // () => this.userNameNotPresented(),
  115. // () => this.userNameNotPresentedHandler()
  116. // ],
  117. [
  118. () => true,
  119. () => this.allStepsPassedHandler()
  120. ]
  121. ]);
  122. const password = this.get('model.password');
  123. const passwordConfirmation = this.get('confirmPassword');
  124. exactChecking(password, passwordConfirmation);
  125. },
  126. actions: {
  127. signUp() {
  128. this.checkIt();
  129. }
  130. }
  131. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement