From 43115da98697a381bba02aefed9ab475951028ff Mon Sep 17 00:00:00 2001 From: Matthieu Riegler Date: Fri, 9 Jun 2023 18:12:45 +0200 Subject: [PATCH] refactor(forms): Log a warning when `FormGroup` keys include a dot. (#50642) Due to the dotted synthax to resolve controls, keys in FormGroups cannot include dots. fixes #50608 PR Close #50642 --- packages/forms/src/model/form_group.ts | 16 ++++++++++++++++ packages/forms/test/form_group_spec.ts | 11 ++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/forms/src/model/form_group.ts b/packages/forms/src/model/form_group.ts index 1028308f08c..30c55930cf7 100644 --- a/packages/forms/src/model/form_group.ts +++ b/packages/forms/src/model/form_group.ts @@ -177,6 +177,7 @@ export class FormGroup( + controls: {[K in keyof TControl]: AbstractControl;}) { + const invalidKeys = Object.keys(controls).filter(key => key.includes('.')); + if (invalidKeys.length > 0) { + // TODO: make this an error once there are no more uses in G3 + console.warn(`FormGroup keys cannot include \`.\`, please replace the keys for: ${ + invalidKeys.join(',')}.`); + } +} + + interface UntypedFormGroupCtor { new(controls: {[key: string]: AbstractControl}, validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null, diff --git a/packages/forms/test/form_group_spec.ts b/packages/forms/test/form_group_spec.ts index df74e8f99f0..db4593fe4c1 100644 --- a/packages/forms/test/form_group_spec.ts +++ b/packages/forms/test/form_group_spec.ts @@ -223,7 +223,6 @@ describe('FormGroup', () => { }); }); - describe('touched', () => { let c: FormControl, g: FormGroup; @@ -2411,5 +2410,15 @@ describe('FormGroup', () => { } }); }); + + it('should throw with invalid keys', () => { + const consoleWarnSpy = spyOn(console, 'warn'); + new FormGroup({ + foo: new FormControl('foo'), + bar: new FormControl('foo', [Validators.required]), + 'baz.not.ok': new FormControl('baz') + }); + expect(consoleWarnSpy).toHaveBeenCalledTimes(1); + }); }); })();