Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forms): error if control is removed as a result of another one being reset #40462

Closed
wants to merge 2 commits into from

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Jan 17, 2021

When a form is reset, it goes through _forEachChild to call reset on each of its children. The problem is that if a control is removed while the loop is running (e.g. by a subscription), the form will throw an error, because it built up the list of available controls before the loop started.

These changes fix the issue by adding a null check before invoking the callback.

Fixes #33401.

…ing reset

When a form is reset, it goes through `_forEachChild` to call `reset` on each of its children.
The problem is that if a control is removed while the loop is running (e.g. by a subscription),
the form will throw an error, because it built up the list of available control before the loop
started.

These changes fix the issue by adding a null check before invoing the callback.

Fixes angular#33401.
@google-cla google-cla bot added the cla: yes label Jan 17, 2021
@crisbeto crisbeto marked this pull request as ready for review January 17, 2021 12:05
@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer area: forms target: patch This PR is targeted for the next patch release type: bug/fix labels Jan 17, 2021
@ngbot ngbot bot modified the milestone: Backlog Jan 17, 2021
@AndrewKushnir
Copy link
Contributor

Thanks for the fix @crisbeto! The original problem comes from the fact that some structural changes happen to the data structure (i.e. new controls added or old ones removed) within the valueChanges subscription fn, which is executed in the middle of the reset operation (in a sense it's similar to starting a new change detection loop while the current one is in progress). I think the current fix is good and we should definitely land it (I will take a look at the fix asap) and later on look at solving the underlying issue (for ex. by delaying the valueChanges emit and triggering it once the changes to the data structure are completed).

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (just one minor comment), thanks @crisbeto! 👍

packages/forms/src/model.ts Outdated Show resolved Hide resolved
@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jan 19, 2021
@AndrewKushnir
Copy link
Contributor

Presubmit.

@crisbeto crisbeto added the action: merge The PR is ready for merge by the caretaker label Jan 19, 2021
@AndrewKushnir AndrewKushnir added target: rc This PR is targeted for the next release-candidate and removed action: presubmit The PR is in need of a google3 presubmit target: patch This PR is targeted for the next patch release labels Jan 19, 2021
AndrewKushnir pushed a commit that referenced this pull request Jan 19, 2021
…ing reset (#40462)

When a form is reset, it goes through `_forEachChild` to call `reset` on each of its children.
The problem is that if a control is removed while the loop is running (e.g. by a subscription),
the form will throw an error, because it built up the list of available control before the loop
started.

These changes fix the issue by adding a null check before invoing the callback.

Fixes #33401.

PR Close #40462
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: forms cla: yes target: rc This PR is targeted for the next release-candidate type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FormGroup reset() throws error if a control is removed based on a valueChanges subscription
2 participants