Skip to content

Commit 51a5baa

Browse files
JeanMecheatscott
authored andcommitted
fix(forms): reset() call with null values on nested group (#48830)
Non typed forms allow to pass null to nested groups when calling `formGroup.reset()`, this commit prevent an undefined access. fixes #20509 PR Close #48830
1 parent 7d8b3f4 commit 51a5baa

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/forms/src/model/form_group.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,9 @@ export class FormGroup<TControl extends {[K in keyof TControl]: AbstractControl<
483483
value: ɵTypedOrUntyped<TControl, ɵFormGroupValue<TControl>, any> = {} as unknown as
484484
ɵFormGroupValue<TControl>,
485485
options: {onlySelf?: boolean, emitEvent?: boolean} = {}): void {
486-
this._forEachChild((control, name) => {
487-
control.reset((value as any)[name], {onlySelf: true, emitEvent: options.emitEvent});
486+
this._forEachChild((control: AbstractControl, name) => {
487+
control.reset(
488+
value ? (value as any)[name] : null, {onlySelf: true, emitEvent: options.emitEvent});
488489
});
489490
this._updatePristine(options);
490491
this._updateTouched(options);

packages/forms/test/form_group_spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,19 @@ describe('FormGroup', () => {
693693
expect(g.disabled).toBe(false);
694694
});
695695

696+
it('should be able to reset a nested control with null', () => {
697+
const g = new FormGroup({
698+
id: new FormControl(2),
699+
nested: new FormGroup<any>({
700+
id: new FormControl(3),
701+
}),
702+
});
703+
704+
g.reset({id: 1, nested: null});
705+
expect(g.get('nested')?.value).toEqual({id: null});
706+
expect(g.get('nested.id')?.value).toBe(null);
707+
});
708+
696709
describe('reset() events', () => {
697710
let form: FormGroup, c3: FormControl, logger: any[];
698711

0 commit comments

Comments
 (0)