Skip to content

Commit

Permalink
fix(forms): reset() call with null values on nested group (#48830)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JeanMeche authored and atscott committed Oct 10, 2023
1 parent 7d8b3f4 commit 51a5baa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/forms/src/model/form_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,9 @@ export class FormGroup<TControl extends {[K in keyof TControl]: AbstractControl<
value: ɵTypedOrUntyped<TControl, ɵFormGroupValue<TControl>, any> = {} as unknown as
ɵFormGroupValue<TControl>,
options: {onlySelf?: boolean, emitEvent?: boolean} = {}): void {
this._forEachChild((control, name) => {
control.reset((value as any)[name], {onlySelf: true, emitEvent: options.emitEvent});
this._forEachChild((control: AbstractControl, name) => {
control.reset(
value ? (value as any)[name] : null, {onlySelf: true, emitEvent: options.emitEvent});
});
this._updatePristine(options);
this._updateTouched(options);
Expand Down
13 changes: 13 additions & 0 deletions packages/forms/test/form_group_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,19 @@ describe('FormGroup', () => {
expect(g.disabled).toBe(false);
});

it('should be able to reset a nested control with null', () => {
const g = new FormGroup({
id: new FormControl(2),
nested: new FormGroup<any>({
id: new FormControl(3),
}),
});

g.reset({id: 1, nested: null});
expect(g.get('nested')?.value).toEqual({id: null});
expect(g.get('nested.id')?.value).toBe(null);
});

describe('reset() events', () => {
let form: FormGroup, c3: FormControl, logger: any[];

Expand Down

0 comments on commit 51a5baa

Please sign in to comment.