refactor(forms): make Validators.group and Validators.array private

This commit is contained in:
vsavkin 2015-10-27 14:35:46 -07:00 committed by Victor Savkin
parent 42ea31b993
commit f98faf0702
4 changed files with 34 additions and 79 deletions

View File

@ -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); });
}
}
}

View File

@ -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;
}
}

View File

@ -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),

View File

@ -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);
});
});
});
}