Skip to content

Commit 43115da

Browse files
JeanMechedylhunn
authored andcommitted
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
1 parent 7981aad commit 43115da

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/forms/src/model/form_group.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export class FormGroup<TControl extends {[K in keyof TControl]: AbstractControl<
177177
controls: TControl, validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
178178
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null) {
179179
super(pickValidators(validatorOrOpts), pickAsyncValidators(asyncValidator, validatorOrOpts));
180+
(typeof ngDevMode === 'undefined' || ngDevMode) && validateFormGroupControls(controls);
180181
this.controls = controls;
181182
this._initObservables();
182183
this._setUpdateStrategy(validatorOrOpts);
@@ -588,6 +589,21 @@ export class FormGroup<TControl extends {[K in keyof TControl]: AbstractControl<
588589
}
589590
}
590591

592+
/**
593+
* Will validate that none of the controls has a key with a dot
594+
* Throws other wise
595+
*/
596+
function validateFormGroupControls<TControl>(
597+
controls: {[K in keyof TControl]: AbstractControl<any, any>;}) {
598+
const invalidKeys = Object.keys(controls).filter(key => key.includes('.'));
599+
if (invalidKeys.length > 0) {
600+
// TODO: make this an error once there are no more uses in G3
601+
console.warn(`FormGroup keys cannot include \`.\`, please replace the keys for: ${
602+
invalidKeys.join(',')}.`);
603+
}
604+
}
605+
606+
591607
interface UntypedFormGroupCtor {
592608
new(controls: {[key: string]: AbstractControl},
593609
validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,

packages/forms/test/form_group_spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ describe('FormGroup', () => {
223223
});
224224
});
225225

226-
227226
describe('touched', () => {
228227
let c: FormControl, g: FormGroup;
229228

@@ -2411,5 +2410,15 @@ describe('FormGroup', () => {
24112410
}
24122411
});
24132412
});
2413+
2414+
it('should throw with invalid keys', () => {
2415+
const consoleWarnSpy = spyOn(console, 'warn');
2416+
new FormGroup({
2417+
foo: new FormControl('foo'),
2418+
bar: new FormControl('foo', [Validators.required]),
2419+
'baz.not.ok': new FormControl('baz')
2420+
});
2421+
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
2422+
});
24142423
});
24152424
})();

0 commit comments

Comments
 (0)