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