Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { autoinject } from 'aurelia-framework';
- import { ValidationRules, ValidationController } from 'aurelia-validation';
- @autoinject
- export class Validate {
- numberField: number;
- controller: any;
- constructor (
- private validationController: ValidationController
- ) {
- this.controller = validationController;
- ValidationRules.customRule(
- 'nummer',
- (value, obj, min, max) => {
- let numberValue: number = parseInt(value);
- return value === null || value === undefined || Number.isInteger(nummer) && nummer >= min && nummer <= max;
- },
- `${$displayName} must be an integer between ${$config.min} and ${$config.max}.`,
- (min, max) => ({ min, max })
- );
- ValidationRules
- .ensure((m: this) => m.numberField)
- .required()
- .withMessage('Rutan får inte vara tom.')
- .satisfiesRule('nummer', 1, 100)
- /*.satisfies((value: any, object?: any) => {
- console.log(value);
- if (value >= 1 && value <= 100) {
- return true;
- }
- else {
- return false;
- }
- })
- .withMessage(`The number must be between - 100.`)*/
- .ensure((m: this) => m.numberField).required()
- .on(this);
- }
- submit(): void {
- this.executeValidation();
- }
- executeValidation(): void {
- this.controller.validate()
- .then(errors => {
- if (errors.length === 0) {
- console.log('all good!');
- } else {
- console.log('all errors!');
- }
- });
- }
- }
- template
- section.au-animate
- h2 Validate
- form(role="form")
- .form-group
- label Number
- input#meetingSubject.form-control(type="text" value.bind="numberField & validate")
- button.btn.btn-lg.btn-primary(click.delegate='submit()') Validate
- div
- h3 ${'latestValidationResult'}
- ul(if.bind='controller.errors.length')
- li(repeat.for='error of controller.errors') ${error.message}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement