Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import { autoinject } from 'aurelia-framework';
  2. import { ValidationRules, ValidationController } from 'aurelia-validation';
  3.  
  4. @autoinject
  5. export class Validate {
  6. numberField: number;
  7.  
  8. controller: any;
  9.  
  10. constructor (
  11. private validationController: ValidationController
  12. ) {
  13. this.controller = validationController;
  14.  
  15. ValidationRules.customRule(
  16. 'nummer',
  17. (value, obj, min, max) => {
  18. let numberValue: number = parseInt(value);
  19. return value === null || value === undefined || Number.isInteger(nummer) && nummer >= min && nummer <= max;
  20. },
  21. `${$displayName} must be an integer between ${$config.min} and ${$config.max}.`,
  22. (min, max) => ({ min, max })
  23. );
  24.  
  25. ValidationRules
  26. .ensure((m: this) => m.numberField)
  27. .required()
  28. .withMessage('Rutan får inte vara tom.')
  29. .satisfiesRule('nummer', 1, 100)
  30. /*.satisfies((value: any, object?: any) => {
  31. console.log(value);
  32. if (value >= 1 && value <= 100) {
  33. return true;
  34. }
  35. else {
  36. return false;
  37. }
  38. })
  39. .withMessage(`The number must be between - 100.`)*/
  40. .ensure((m: this) => m.numberField).required()
  41. .on(this);
  42. }
  43.  
  44. submit(): void {
  45. this.executeValidation();
  46. }
  47.  
  48. executeValidation(): void {
  49. this.controller.validate()
  50. .then(errors => {
  51. if (errors.length === 0) {
  52. console.log('all good!');
  53. } else {
  54. console.log('all errors!');
  55. }
  56. });
  57. }
  58.  
  59. }
  60.  
  61. template
  62. section.au-animate
  63. h2 Validate
  64.  
  65. form(role="form")
  66. .form-group
  67. label Number
  68. input#meetingSubject.form-control(type="text" value.bind="numberField & validate")
  69.  
  70. button.btn.btn-lg.btn-primary(click.delegate='submit()') Validate
  71. div
  72. h3 ${'latestValidationResult'}
  73. ul(if.bind='controller.errors.length')
  74. li(repeat.for='error of controller.errors') ${error.message}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement