Skip to content

Commit

Permalink
fix(forms): reset() call with null values on nested group (angular#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 angular#20509

PR Close angular#48830
  • Loading branch information
JeanMeche authored and ChellappanRajan committed Jan 23, 2024
1 parent c83326e commit 46c13c5
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 @@ -485,8 +485,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 @@ -694,6 +694,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 46c13c5

Please sign in to comment.