feat(forms): add support for validations

This commit is contained in:
vsavkin
2015-02-11 11:10:31 -08:00
parent 65ebff056a
commit ded83e589b
10 changed files with 283 additions and 8 deletions
+31
View File
@@ -0,0 +1,31 @@
import {isBlank, isPresent} from 'angular2/src/facade/lang';
import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
import {ControlGroup, Control} from 'angular2/forms';
export function required(c:Control) {
return isBlank(c.value) || c.value === "" ? {"required" : true} : null;
}
export function nullValidator(c:Control) {
return null;
}
export function compose(validators:List<Function>):Function {
return function(c:Control) {
return ListWrapper.reduce(validators, (res, validator) => {
var errors = validator(c);
return isPresent(errors) ? StringMapWrapper.merge(res, errors) : res;
}, {});
}
}
export function controlGroupValidator(c:ControlGroup) {
var res = {};
StringMapWrapper.forEach(c.controls, (control, name) => {
if (isPresent(control.errors)) {
res[name] = control.errors;
}
});
return StringMapWrapper.isEmpty(res) ? null : res;
}