Permalink
Browse files

feat(Form): the form class which makes writing form a bit easier

  • Loading branch information...
bas080
bas080 committed Jun 8, 2016
1 parent 2cb7214 commit 71b5b869566ad1561d328d3195d43c7f9de53639
Showing with 47 additions and 0 deletions.
  1. +47 −0 src/form.js
@@ -0,0 +1,47 @@
import {Validator, ValidationEngine} from 'aurelia-validatejs';

/***
* Some functionalities are desired in a view model of a form. Responsibilites
* include validation and submit.
*
* This class is optional and serves as a convenience
*/
export class Form {

messages = {}

_model

constructor() {}

set model(newModel) {
this._model = newModel;
this.validator = new Validator(newModel);
this.reporter = ValidationEngine.getValidationReporter(newModel);
this.observer = this.reporter.subscribe(validationErrors => {
this.messages = validationErrors.reduce( (acc, error) => {
acc[error.propertyName] = error.message;
return acc;
}, {});
});
}

get model() {
return this._model;
}

validate() {
this.validator.validate();
}

detached() {
this.observer.dispose();
}

submit() {
}

change() {
this.validate();
}
}

0 comments on commit 71b5b86

Please sign in to comment.