aurelia-validatejs
Version:
This is a plugin that will allow using validate.js in your Aurelia application for expressive validation.
51 lines (48 loc) • 1.51 kB
JavaScript
import {inject} from 'aurelia-framework';
import {length, required, date, datetime, email, equality, exclusion, inclusion, format, url, numericality} from 'aurelia-validatejs';
import {ValidationEngine} from 'aurelia-validatejs';
export class Decorators {
model;
errors = [];
subscriber;
constructor() {
this.model = new Model();
this.reporter = ValidationEngine.getValidationReporter(this.model);
this.subscriber = this.reporter.subscribe(result => {
this.renderErrors(result);
});
}
detached() {
this.subscriber.dispose();
}
submit() {
if (!this.hasErrors()) {
alert('Submitted successfully');
} else {
alert('Form has errors');
}
}
hasErrors() {
return !!this.errors.length;
}
renderErrors(result) {
this.errors.splice(0, this.errors.length);
result.forEach(error => {
this.errors.push(error)
});
}
}
class Model {
firstName = 'Luke';
lastName = 'Skywalker';
// @date lastUpdated = new Date();
// @datetime lastTimeUpdated = new Date();
email = 'luke@skywalker.net';
password = 'equal';
confirmPassword = 'equal';
blueOrRed = 'yellow';
gender = 'male';
website = 'http://www.google.com';
age = 25;
zipCode = '12345';
}