X Tutup
Skip to content

Commit 7343ef0

Browse files
committed
feat(forms): remove controlsErrors
BREAKING CHANGE Previously, the controlsErrors getter of ControlGroup and ControlArray returned the errors of their direct children. This was confusing because the result did not include the errors of nested children (ControlGroup -> ControlGroup -> Control). Making controlsErrors to include such errors would require inventing some custom serialization format, which applications would have to understand. Since controlsErrors was just a convenience method, and it was causing confusing, we are removing it. If you want to get the errors of the whole form serialized into a single object, you can manually traverse the form and accumulate the errors. This way you have more control over how the errors are serialized. Closes angular#5102
1 parent 4439106 commit 7343ef0

File tree

4 files changed

+1
-123
lines changed

4 files changed

+1
-123
lines changed

modules/angular2/src/common/forms/directives/abstract_control_directive.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export abstract class AbstractControlDirective {
1818
return isPresent(this.control) ? this.control.errors : null;
1919
}
2020

21-
get controlsErrors(): any { return isPresent(this.control) ? this.control.controlsErrors : null; }
22-
2321
get pristine(): boolean { return isPresent(this.control) ? this.control.pristine : null; }
2422

2523
get dirty(): boolean { return isPresent(this.control) ? this.control.dirty : null; }

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

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export abstract class AbstractControl {
5858
private _statusChanges: EventEmitter<any>;
5959
private _status: string;
6060
private _errors: {[key: string]: any};
61-
private _controlsErrors: any;
6261
private _pristine: boolean = true;
6362
private _touched: boolean = false;
6463
private _parent: ControlGroup | ControlArray;
@@ -77,11 +76,6 @@ export abstract class AbstractControl {
7776
*/
7877
get errors(): {[key: string]: any} { return this._errors; }
7978

80-
/**
81-
* Returns the errors of the child controls.
82-
*/
83-
get controlsErrors(): any { return this._controlsErrors; }
84-
8579
get pristine(): boolean { return this._pristine; }
8680

8781
get dirty(): boolean { return !this.pristine; }
@@ -126,7 +120,6 @@ export abstract class AbstractControl {
126120
this._updateValue();
127121

128122
this._errors = this._runValidator();
129-
this._controlsErrors = this._calculateControlsErrors();
130123
this._status = this._calculateStatus();
131124

132125
if (this._status == VALID || this._status == PENDING) {
@@ -216,7 +209,6 @@ export abstract class AbstractControl {
216209

217210
/** @internal */
218211
_updateControlsErrors(): void {
219-
this._controlsErrors = this._calculateControlsErrors();
220212
this._status = this._calculateStatus();
221213

222214
if (isPresent(this._parent)) {
@@ -240,8 +232,7 @@ export abstract class AbstractControl {
240232

241233
/** @internal */
242234
abstract _updateValue(): void;
243-
/** @internal */
244-
abstract _calculateControlsErrors(): any;
235+
245236
/** @internal */
246237
abstract _anyControlsHaveStatus(status: string): boolean;
247238
}
@@ -301,11 +292,6 @@ export class Control extends AbstractControl {
301292
*/
302293
_updateValue() {}
303294

304-
/**
305-
* @internal
306-
*/
307-
_calculateControlsErrors() { return null; }
308-
309295
/**
310296
* @internal
311297
*/
@@ -388,17 +374,6 @@ export class ControlGroup extends AbstractControl {
388374
/** @internal */
389375
_updateValue() { this._value = this._reduceValue(); }
390376

391-
/** @internal */
392-
_calculateControlsErrors() {
393-
var res = {};
394-
StringMapWrapper.forEach(this.controls, (control, name) => {
395-
if (this.contains(name) && isPresent(control.errors)) {
396-
res[name] = control.errors;
397-
}
398-
});
399-
return StringMapWrapper.isEmpty(res) ? null : res;
400-
}
401-
402377
/** @internal */
403378
_anyControlsHaveStatus(status: string): boolean {
404379
var res = false;
@@ -503,19 +478,6 @@ export class ControlArray extends AbstractControl {
503478
/** @internal */
504479
_updateValue(): void { this._value = this.controls.map((control) => control.value); }
505480

506-
/** @internal */
507-
_calculateControlsErrors() {
508-
var res = [];
509-
var anyErrors = false;
510-
this.controls.forEach((control) => {
511-
res.push(control.errors);
512-
if (isPresent(control.errors)) {
513-
anyErrors = true;
514-
}
515-
});
516-
return anyErrors ? res : null;
517-
}
518-
519481
/** @internal */
520482
_anyControlsHaveStatus(status: string): boolean {
521483
return ListWrapper.any(this.controls, c => c.status == status);

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

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ export function main() {
302302

303303
c.setErrors({"someError": true});
304304

305-
expect(g.controlsErrors).toEqual({"one": {"someError": true}});
306305
expect(g.valid).toEqual(false);
307306
});
308307

@@ -354,41 +353,6 @@ export function main() {
354353
});
355354
});
356355

357-
describe("controlsErrors", () => {
358-
it("should be null when no errors", () => {
359-
var g = new ControlGroup({"one": new Control('value', Validators.required)});
360-
361-
expect(g.valid).toEqual(true);
362-
expect(g.controlsErrors).toEqual(null);
363-
});
364-
365-
it("should collect errors from the child controls", () => {
366-
var one = new Control(null, Validators.required);
367-
var g = new ControlGroup({"one": one});
368-
369-
expect(g.valid).toEqual(false);
370-
expect(g.controlsErrors).toEqual({"one": {"required": true}});
371-
});
372-
373-
it("should not include controls that have no errors", () => {
374-
var one = new Control(null, Validators.required);
375-
var two = new Control("two");
376-
var g = new ControlGroup({"one": one, "two": two});
377-
378-
expect(g.controlsErrors).toEqual({"one": {"required": true}});
379-
});
380-
381-
it("should run the validator with the value changes", () => {
382-
var c = new Control(null, Validators.required);
383-
var g = new ControlGroup({"one": c});
384-
385-
c.updateValue("some value");
386-
387-
expect(g.valid).toEqual(true);
388-
expect(g.controlsErrors).toEqual(null);
389-
});
390-
});
391-
392356
describe("errors", () => {
393357
it("should run the validator when the value changes", () => {
394358
var simpleValidator = (c) =>
@@ -667,39 +631,6 @@ export function main() {
667631
});
668632
});
669633

670-
describe("controlsErrors", () => {
671-
it("should return null when no errors", () => {
672-
var a = new ControlArray(
673-
[new Control(1, Validators.required), new Control(2, Validators.required)]);
674-
675-
expect(a.valid).toBe(true);
676-
expect(a.controlsErrors).toBe(null);
677-
});
678-
679-
it("should collect errors from the child controls", () => {
680-
var a = new ControlArray([
681-
new Control(1, Validators.required),
682-
new Control(null, Validators.required),
683-
new Control(2, Validators.required)
684-
]);
685-
686-
expect(a.valid).toBe(false);
687-
expect(a.controlsErrors).toEqual([null, {"required": true}, null]);
688-
});
689-
690-
it("should run the validator when the value changes", () => {
691-
var a = new ControlArray([]);
692-
var c = new Control(null, Validators.required);
693-
a.push(c);
694-
expect(a.valid).toBe(false);
695-
696-
c.updateValue("some value");
697-
698-
expect(a.valid).toBe(true);
699-
expect(a.controlsErrors).toBe(null);
700-
});
701-
});
702-
703634
describe("errors", () => {
704635
it("should run the validator when the value changes", () => {
705636
var simpleValidator = (c) => c.controls[0].value != "correct" ? {"broken": true} : null;

modules/angular2/test/public_api_spec.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ var NG_ALL = [
4343
'AbstractControl',
4444
'AbstractControl.dirty',
4545
'AbstractControl.errors',
46-
'AbstractControl.controlsErrors',
4746
'AbstractControl.find()',
4847
'AbstractControl.getError()',
4948
'AbstractControl.hasError()',
@@ -70,7 +69,6 @@ var NG_ALL = [
7069
'AbstractControlDirective.control',
7170
'AbstractControlDirective.dirty',
7271
'AbstractControlDirective.errors',
73-
'AbstractControlDirective.controlsErrors',
7472
'AbstractControlDirective.pristine',
7573
'AbstractControlDirective.touched',
7674
'AbstractControlDirective.untouched',
@@ -285,7 +283,6 @@ var NG_ALL = [
285283
'Control',
286284
'Control.dirty',
287285
'Control.errors',
288-
'Control.controlsErrors',
289286
'Control.find()',
290287
'Control.getError()',
291288
'Control.hasError()',
@@ -316,7 +313,6 @@ var NG_ALL = [
316313
'ControlArray.controls=',
317314
'ControlArray.dirty',
318315
'ControlArray.errors',
319-
'ControlArray.controlsErrors',
320316
'ControlArray.find()',
321317
'ControlArray.getError()',
322318
'ControlArray.hasError()',
@@ -347,7 +343,6 @@ var NG_ALL = [
347343
'ControlContainer.control',
348344
'ControlContainer.dirty',
349345
'ControlContainer.errors',
350-
'ControlContainer.controlsErrors',
351346
'ControlContainer.formDirective',
352347
'ControlContainer.name',
353348
'ControlContainer.name=',
@@ -364,7 +359,6 @@ var NG_ALL = [
364359
'ControlGroup.controls=',
365360
'ControlGroup.dirty',
366361
'ControlGroup.errors',
367-
'ControlGroup.controlsErrors',
368362
'ControlGroup.exclude()',
369363
'ControlGroup.find()',
370364
'ControlGroup.getError()',
@@ -836,7 +830,6 @@ var NG_ALL = [
836830
'NgControl.control',
837831
'NgControl.dirty',
838832
'NgControl.errors',
839-
'NgControl.controlsErrors',
840833
'NgControl.name',
841834
'NgControl.name=',
842835
'NgControl.path',
@@ -853,7 +846,6 @@ var NG_ALL = [
853846
'NgControlGroup.control',
854847
'NgControlGroup.dirty',
855848
'NgControlGroup.errors',
856-
'NgControlGroup.controlsErrors',
857849
'NgControlGroup.formDirective',
858850
'NgControlGroup.name',
859851
'NgControlGroup.name=',
@@ -878,7 +870,6 @@ var NG_ALL = [
878870
'NgControlName.control',
879871
'NgControlName.dirty',
880872
'NgControlName.errors',
881-
'NgControlName.controlsErrors',
882873
'NgControlName.formDirective',
883874
'NgControlName.model',
884875
'NgControlName.model=',
@@ -912,7 +903,6 @@ var NG_ALL = [
912903
'NgForm.controls',
913904
'NgForm.dirty',
914905
'NgForm.errors',
915-
'NgForm.controlsErrors',
916906
'NgForm.form',
917907
'NgForm.form=',
918908
'NgForm.formDirective',
@@ -936,7 +926,6 @@ var NG_ALL = [
936926
'NgFormControl.control',
937927
'NgFormControl.dirty',
938928
'NgFormControl.errors',
939-
'NgFormControl.controlsErrors',
940929
'NgFormControl.form',
941930
'NgFormControl.form=',
942931
'NgFormControl.model',
@@ -967,7 +956,6 @@ var NG_ALL = [
967956
'NgFormModel.directives=',
968957
'NgFormModel.dirty',
969958
'NgFormModel.errors',
970-
'NgFormModel.controlsErrors',
971959
'NgFormModel.form',
972960
'NgFormModel.form=',
973961
'NgFormModel.formDirective',
@@ -994,7 +982,6 @@ var NG_ALL = [
994982
'NgModel.control',
995983
'NgModel.dirty',
996984
'NgModel.errors',
997-
'NgModel.controlsErrors',
998985
'NgModel.model',
999986
'NgModel.model=',
1000987
'NgModel.name',

0 commit comments

Comments
 (0)
X Tutup