From c2ceb7fba4c6bca0dd1490b8402f4f7dd1ad4c2d Mon Sep 17 00:00:00 2001 From: ericmartinezr Date: Thu, 21 Jan 2016 10:25:31 -0300 Subject: [PATCH] fix(Validators): fix Validators.required marking number zero as invalid Closes #6617 --- modules/angular2/src/common/forms/validators.ts | 6 ++++-- modules/angular2/test/common/forms/validators_spec.ts | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/angular2/src/common/forms/validators.ts b/modules/angular2/src/common/forms/validators.ts index f9cdc1c647..3cc476ca71 100644 --- a/modules/angular2/src/common/forms/validators.ts +++ b/modules/angular2/src/common/forms/validators.ts @@ -1,4 +1,4 @@ -import {isBlank, isPresent, CONST_EXPR} from 'angular2/src/facade/lang'; +import {isBlank, isPresent, CONST_EXPR, isString} from 'angular2/src/facade/lang'; import {PromiseWrapper} from 'angular2/src/facade/promise'; import {ObservableWrapper} from 'angular2/src/facade/async'; import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection'; @@ -44,7 +44,9 @@ export class Validators { * Validator that requires controls to have a non-empty value. */ static required(control: modelModule.Control): {[key: string]: boolean} { - return isBlank(control.value) || control.value == "" ? {"required": true} : null; + return isBlank(control.value) || (isString(control.value) && control.value == "") ? + {"required": true} : + null; } /** diff --git a/modules/angular2/test/common/forms/validators_spec.ts b/modules/angular2/test/common/forms/validators_spec.ts index 5a1e13698c..2ce72b87aa 100644 --- a/modules/angular2/test/common/forms/validators_spec.ts +++ b/modules/angular2/test/common/forms/validators_spec.ts @@ -35,6 +35,9 @@ export function main() { it("should not error on a non-empty string", () => { expect(Validators.required(new Control("not empty"))).toEqual(null); }); + + it("should accept zero as valid", + () => { expect(Validators.required(new Control(0))).toEqual(null); }); }); describe("minLength", () => {