Skip to content

Commit c2ceb7f

Browse files
committed
fix(Validators): fix Validators.required marking number zero as invalid
Closes #6617
1 parent 4bfe49c commit c2ceb7f

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

modules/angular2/src/common/forms/validators.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isBlank, isPresent, CONST_EXPR} from 'angular2/src/facade/lang';
1+
import {isBlank, isPresent, CONST_EXPR, isString} from 'angular2/src/facade/lang';
22
import {PromiseWrapper} from 'angular2/src/facade/promise';
33
import {ObservableWrapper} from 'angular2/src/facade/async';
44
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
@@ -44,7 +44,9 @@ export class Validators {
4444
* Validator that requires controls to have a non-empty value.
4545
*/
4646
static required(control: modelModule.Control): {[key: string]: boolean} {
47-
return isBlank(control.value) || control.value == "" ? {"required": true} : null;
47+
return isBlank(control.value) || (isString(control.value) && control.value == "") ?
48+
{"required": true} :
49+
null;
4850
}
4951

5052
/**

modules/angular2/test/common/forms/validators_spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export function main() {
3535

3636
it("should not error on a non-empty string",
3737
() => { expect(Validators.required(new Control("not empty"))).toEqual(null); });
38+
39+
it("should accept zero as valid",
40+
() => { expect(Validators.required(new Control(0))).toEqual(null); });
3841
});
3942

4043
describe("minLength", () => {

0 commit comments

Comments
 (0)