diff --git a/modules/angular2/src/core/forms/model.ts b/modules/angular2/src/core/forms/model.ts index 8dcef80501..9b6c11baaa 100644 --- a/modules/angular2/src/core/forms/model.ts +++ b/modules/angular2/src/core/forms/model.ts @@ -321,7 +321,15 @@ export class ControlGroup extends AbstractControl { _updateValue() { this._value = this._reduceValue(); } /** @internal */ - _calculateControlsErrors() { return Validators.group(this); } + _calculateControlsErrors() { + var res = {}; + StringMapWrapper.forEach(this.controls, (control, name) => { + if (this.contains(name) && isPresent(control.errors)) { + res[name] = control.errors; + } + }); + return StringMapWrapper.isEmpty(res) ? null : res; + } /** @internal */ _reduceValue() { @@ -420,10 +428,20 @@ export class ControlArray extends AbstractControl { _updateValue(): void { this._value = this.controls.map((control) => control.value); } /** @internal */ - _calculateControlsErrors() { return Validators.array(this); } + _calculateControlsErrors() { + var res = []; + var anyErrors = false; + this.controls.forEach((control) => { + res.push(control.errors); + if (isPresent(control.errors)) { + anyErrors = true; + } + }); + return anyErrors ? res : null; + } /** @internal */ _setParentForControls(): void { this.controls.forEach((control) => { control.setParent(this); }); } -} +} \ No newline at end of file diff --git a/modules/angular2/src/core/forms/validators.ts b/modules/angular2/src/core/forms/validators.ts index 2f4a1a5c1a..d93735fc6c 100644 --- a/modules/angular2/src/core/forms/validators.ts +++ b/modules/angular2/src/core/forms/validators.ts @@ -54,26 +54,4 @@ export class Validators { return StringMapWrapper.isEmpty(res) ? null : res; }; } - - static group(group: modelModule.ControlGroup): {[key: string]: any} { - var res: {[key: string]: any[]} = {}; - StringMapWrapper.forEach(group.controls, (control, name) => { - if (group.contains(name) && isPresent(control.errors)) { - res[name] = control.errors; - } - }); - return StringMapWrapper.isEmpty(res) ? null : res; - } - - static array(array: modelModule.ControlArray): any[] { - var res: any[] = []; - var anyErrors: boolean = false; - array.controls.forEach((control) => { - res.push(control.errors); - if (isPresent(control.errors)) { - anyErrors = true; - } - }); - return anyErrors ? res : null; - } } diff --git a/modules/angular2/test/core/forms/model_spec.ts b/modules/angular2/test/core/forms/model_spec.ts index 1e5cd52f67..810f305446 100644 --- a/modules/angular2/test/core/forms/model_spec.ts +++ b/modules/angular2/test/core/forms/model_spec.ts @@ -193,7 +193,7 @@ export function main() { expect(g.errors).toEqual({"someGroupError": true}); }); - it("update a value should reset errosr", () => { + it("should reset errors when updating a value", () => { var c = new Control("oldValue"); var g = new ControlGroup({"one": c}); @@ -232,14 +232,14 @@ export function main() { }); describe("controlsErrors", () => { - it("should run the validator with the initial value (valid)", () => { + it("should be null when no errors", () => { var g = new ControlGroup({"one": new Control('value', Validators.required)}); expect(g.valid).toEqual(true); expect(g.controlsErrors).toEqual(null); }); - it("should run the validator with the initial value (invalid)", () => { + it("should collect errors from the child controls", () => { var one = new Control(null, Validators.required); var g = new ControlGroup({"one": one}); @@ -247,6 +247,14 @@ export function main() { expect(g.controlsErrors).toEqual({"one": {"required": true}}); }); + it("should not include controls that have no errors", () => { + var one = new Control(null, Validators.required); + var two = new Control("two"); + var g = new ControlGroup({"one": one, "two": two}); + + expect(g.controlsErrors).toEqual({"one": {"required": true}}); + }); + it("should run the validator with the value changes", () => { var c = new Control(null, Validators.required); var g = new ControlGroup({"one": c}); @@ -501,7 +509,7 @@ export function main() { }); describe("controlsErrors", () => { - it("should run the validator with the initial value (valid)", () => { + it("should return null when no errors", () => { var a = new ControlArray( [new Control(1, Validators.required), new Control(2, Validators.required)]); @@ -509,7 +517,7 @@ export function main() { expect(a.controlsErrors).toBe(null); }); - it("should run the validator with the initial value (invalid)", () => { + it("should collect errors from the child controls", () => { var a = new ControlArray([ new Control(1, Validators.required), new Control(null, Validators.required), diff --git a/modules/angular2/test/core/forms/validators_spec.ts b/modules/angular2/test/core/forms/validators_spec.ts index 86eed1fb3b..6d2826152f 100644 --- a/modules/angular2/test/core/forms/validators_spec.ts +++ b/modules/angular2/test/core/forms/validators_spec.ts @@ -83,54 +83,5 @@ export function main() { expect(c(new Control(""))).toEqual(null); }); }); - - describe("controlGroupValidator", () => { - it("should collect errors from the child controls", () => { - var one = new Control("one", validator("a", true)); - var two = new Control("two", validator("b", true)); - var g = new ControlGroup({"one": one, "two": two}); - - expect(Validators.group(g)).toEqual({"one": {"a": true}, "two": {"b": true}}); - }); - - it("should not include controls that have no errors", () => { - var one = new Control("one", validator("a", true)); - var two = new Control("two"); - var g = new ControlGroup({"one": one, "two": two}); - - expect(Validators.group(g)).toEqual({"one": {"a": true}}); - }); - - it("should return null when no errors", () => { - var g = new ControlGroup({"one": new Control("one")}); - - expect(Validators.group(g)).toEqual(null); - }); - }); - - describe("controlArrayValidator", () => { - it("should collect errors from the child controls", () => { - var one = new Control("one", validator("a", true)); - var two = new Control("two", validator("b", true)); - var a = new ControlArray([one, two]); - - expect(Validators.array(a)).toEqual([{"a": true}, {"b": true}]); - }); - - it("should not include controls that have no errors", () => { - var one = new Control("one"); - var two = new Control("two", validator("a", true)); - var three = new Control("three"); - var a = new ControlArray([one, two, three]); - - expect(Validators.array(a)).toEqual([null, {"a": true}, null]); - }); - - it("should return null when no errors", () => { - var a = new ControlArray([new Control("one")]); - - expect(Validators.array(a)).toEqual(null); - }); - }); }); }