From 9d58f46ea5ad9646dcbaa27209b55ea0008319b8 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Thu, 29 Oct 2015 17:45:24 -0700 Subject: [PATCH] fix(forms): update compose to handle null validators --- modules/angular2/src/core/forms/validators.ts | 5 ++--- modules/angular2/test/core/forms/validators_spec.ts | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/angular2/src/core/forms/validators.ts b/modules/angular2/src/core/forms/validators.ts index bd5b4aaa2d..d96ec40e5b 100644 --- a/modules/angular2/src/core/forms/validators.ts +++ b/modules/angular2/src/core/forms/validators.ts @@ -1,5 +1,4 @@ -import {isBlank, isPresent} from 'angular2/src/core/facade/lang'; -import {CONST_EXPR} from 'angular2/src/core/facade/lang'; +import {isBlank, isPresent, CONST_EXPR} from 'angular2/src/core/facade/lang'; import {ListWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection'; import {OpaqueToken} from 'angular2/src/core/di'; @@ -81,7 +80,7 @@ export class Validators { return function(control: modelModule.AbstractControl) { var res = ListWrapper.reduce(validators, (res, validator) => { - var errors = validator(control); + var errors = isPresent(validator) ? validator(control) : null; return isPresent(errors) ? StringMapWrapper.merge(res, errors) : res; }, {}); return StringMapWrapper.isEmpty(res) ? null : res; diff --git a/modules/angular2/test/core/forms/validators_spec.ts b/modules/angular2/test/core/forms/validators_spec.ts index 6d2826152f..9182fc2ef8 100644 --- a/modules/angular2/test/core/forms/validators_spec.ts +++ b/modules/angular2/test/core/forms/validators_spec.ts @@ -82,6 +82,11 @@ export function main() { var c = Validators.compose([Validators.nullValidator, Validators.nullValidator]); expect(c(new Control(""))).toEqual(null); }); + + it("should ignore nulls", () => { + var c = Validators.compose([null, Validators.required]); + expect(c(new Control(""))).toEqual({"required": true}); + }); }); }); }