Permalink
Browse files

feat(project): enable form validation

  • Loading branch information...
RWOverdijk committed Feb 17, 2017
1 parent e7f21e3 commit f5df44402c75fa058b64dcccd0a05b12411d6b0c
Showing with 125 additions and 0 deletions.
  1. +26 −0 src/ValidationController.js
  2. +75 −0 src/ValidationRenderer.js
  3. +24 −0 src/aurelia-form-validation.js
@@ -0,0 +1,26 @@
import {inject} from 'aurelia-dependency-injection';
import {ValidationControllerFactory, validateTrigger} from 'aurelia-validation';
import {ValidationRenderer} from './ValidationRenderer';
@inject(ValidationControllerFactory)
export class ValidationController {
factory = null;
constructor(validationControllerFactory) {
this.factory = validationControllerFactory;
}
getController(groups, trigger = 0) {
let controller = this.factory.createForCurrentScope();
controller.validateTrigger = trigger;
controller.addRenderer(new ValidationRenderer(groups));
return controller;
}
getTriggers() {
return validateTrigger;
}
}
@@ -0,0 +1,75 @@
import {logger} from './aurelia-form-validation';
export class ValidationRenderer {
groups = {};
constructor(groups) {
this.groups = groups;
}
/**
* Add errors for valid elements.
*
* @param {{message: string, propertyName: string}} result
*/
add(result) {
let group = this.getGroup(result);
if (!group) {
return;
}
group.error = result.message;
group.state = 'error';
}
/**
* Get form group.
*
* @param {string} propertyName
*
* @returns {*}
*/
getGroup({propertyName}) {
let group = this.groups[propertyName];
if (!group) {
logger.warn(`Group not found for validation error on '${propertyName}'.`);
return;
}
return group;
}
/**
* Remove errors for valid elements.
*
* @param {{message: string, propertyName: string}} result
*/
remove(result) {
let group = this.getGroup(result);
if (!group) {
return;
}
group.error = '';
group.state = 'success';
}
/**
* Render (or un-render) errors.
*
* @param {{render: {}, unrender: {}}} instructions
*/
render(instructions) {
for (let {error} of instructions.unrender) {
this.remove(error);
}
for (let {error} of instructions.render) {
this.add(error);
}
}
}
@@ -0,0 +1,24 @@
import {getLogger} from 'aurelia-logging';
import {Configuration} from 'aurelia-config';
import {validateTrigger} from 'aurelia-validation';
import {ValidationController} from './ValidationController';
export const logger = getLogger('aurelia-form-validation');
export function configure(aurelia, config) {
let formConfig = aurelia.container.get(Configuration.of('aurelia-form'));
if (formConfig.validation.controller) {
return;
}
config.validation.controller = aurelia.container.get(ValidationController);
}
export const config = {
'aurelia-form': {
validation: {
trigger: validateTrigger.change
}
}
};

0 comments on commit f5df444

Please sign in to comment.