38 lines
1.2 KiB
JavaScript
Raw Normal View History

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) {
2015-02-24 11:59:10 -08:00
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) {
2015-02-24 11:59:10 -08:00
var res = ListWrapper.reduce(validators, (res, validator) => {
var errors = validator(c);
return isPresent(errors) ? StringMapWrapper.merge(res, errors) : res;
}, {});
2015-02-24 11:59:10 -08:00
return StringMapWrapper.isEmpty(res) ? null : res;
}
}
export function controlGroupValidator(c:ControlGroup) {
var res = {};
StringMapWrapper.forEach(c.controls, (control, name) => {
2015-02-24 11:59:10 -08:00
if (control.active && isPresent(control.errors)) {
2015-02-25 12:54:27 -08:00
StringMapWrapper.forEach(control.errors, (value, error) => {
if (! StringMapWrapper.contains(res, error)) {
res[error] = [];
}
ListWrapper.push(res[error], control);
});
}
});
return StringMapWrapper.isEmpty(res) ? null : res;
}