feat(forms): add optional controls
This commit is contained in:
+4
-3
@@ -4,7 +4,7 @@ import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collectio
|
||||
import {ControlGroup, Control} from 'angular2/forms';
|
||||
|
||||
export function required(c:Control) {
|
||||
return isBlank(c.value) || c.value === "" ? {"required" : true} : null;
|
||||
return isBlank(c.value) || c.value == "" ? {"required" : true} : null;
|
||||
}
|
||||
|
||||
export function nullValidator(c:Control) {
|
||||
@@ -13,17 +13,18 @@ export function nullValidator(c:Control) {
|
||||
|
||||
export function compose(validators:List<Function>):Function {
|
||||
return function(c:Control) {
|
||||
return ListWrapper.reduce(validators, (res, validator) => {
|
||||
var res = ListWrapper.reduce(validators, (res, validator) => {
|
||||
var errors = validator(c);
|
||||
return isPresent(errors) ? StringMapWrapper.merge(res, errors) : res;
|
||||
}, {});
|
||||
return StringMapWrapper.isEmpty(res) ? null : res;
|
||||
}
|
||||
}
|
||||
|
||||
export function controlGroupValidator(c:ControlGroup) {
|
||||
var res = {};
|
||||
StringMapWrapper.forEach(c.controls, (control, name) => {
|
||||
if (isPresent(control.errors)) {
|
||||
if (control.active && isPresent(control.errors)) {
|
||||
res[name] = control.errors;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user