Skip to content

fix(forms): clear parent when removing controls (#29517) - #29580

Closed
mbinic wants to merge 1 commit into
angular:masterfrom
mbinic:fix_forms_29517
Closed

fix(forms): clear parent when removing controls (#29517)#29580
mbinic wants to merge 1 commit into
angular:masterfrom
mbinic:fix_forms_29517

Conversation

@mbinic

@mbinic mbinic commented Mar 29, 2019

Copy link
Copy Markdown

PR Checklist

PR Type

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

When a control is removed from a FormGroup (removeControl), or replaced by another control (setControl), that control's parent is not cleared, i.e. the removed control continues to reference the old parent.

Issue Number: #29517

What is the new behavior?

When a control is removed or replaced, its parent is set to null.

Does this PR introduce a breaking change?

  • Yes
  • No

If you were relying on the removed/replaced controls to retain the old parent reference, this would be a breaking change. This kind of usage seems like a hack, but to get it working, one could store the old parent reference prior to calling removeControl/setControl or in an overridden setParent method.

Other information

Docs for the new unregisterControl method have been added in the code file (forms/model.ts).

When a control is removed from a FormGroup, or replaced by another control, that control's parent
wasn't cleared. Apart from leaving the parent reference on the removed control, this also made it
impossible for the control to react on its removal in an overridden setParent.

This change introduces an unregisterControl method, symmetric to registerControl, which is called
to clear a control's parent (set to null) when it's removed or replaced.

PR Close #29517
@mbinic
mbinic requested a review from a team March 29, 2019 01:44
@mbinic

mbinic commented Mar 29, 2019

Copy link
Copy Markdown
Author

The first failing check "ci/circleci:test" is a result of the public API change (public_api_guard:forms_api), which is expected and should be fine.
The second one, "ci/circleci: test_docs_examples", doesn't seem to be related to my changes as the failing test is "Lifecycle hooks - should support DoCheck hook". It might be that my branch just inherited something faulty from master.

@ngbot ngbot Bot added this to the needsTriage milestone Apr 1, 2019
@mbinic

mbinic commented Apr 14, 2019

Copy link
Copy Markdown
Author

@fw-forms, please review, I'd be happy to address any issues and concerns :)

@itbrandonsilva

itbrandonsilva commented Jan 31, 2020

Copy link
Copy Markdown

Is there any particular reason the idea proposed by the PR is not valid (not taking into account CI checks)?

@pullapprove
pullapprove Bot requested a review from AndrewKushnir January 31, 2020 19:15
@mbinic

mbinic commented Feb 12, 2020

Copy link
Copy Markdown
Author

Hi @itbrandonsilva,
When writing this, I had no doubts apart from undefined vs null. But, we should most probably go with whatever is most consistent with the rest of the codebase.

TLDR:
One might lean towards undefined because:

  1. When removing a child, its parent is no longer defined, rather than it being a child of null.
  2. When/if someone decides to serialize the state, they would probably omit the parent property, rather than including parent: null.
    Otherwise, I see no practical implications, as one could just use the same check (!parent) and be covered for both.

@AndrewKushnir

AndrewKushnir commented Jun 2, 2020

Copy link
Copy Markdown
Contributor

Hi @mbinic, thanks for creating this PR.

I looked at the PR description and the issue that you refer to and I'd like to discuss it more. Currently when you call removeControl, the control is detached from the abstract control tree (so there are no references to that control from the tree), but it retains all its properties (for ex. errors), so clearing parent seems inconsistent from that perspective. Given the fact that it would also be a breaking change, I'm thinking if we should instead just extend the setParent to accept null as a possible argument, so the parent can be cleared if it's needed in a particular use-case. We will also keep your original ticket open to collect more feedback from the community to see if there are some use-cases where clearing parent after detaching a control is a blocker. What do you think?

Thank you.

@AndrewKushnir

Copy link
Copy Markdown
Contributor

Closing this PR for now as this change may cause some inconsistencies with other APIs (see #29580 (comment)). I think it'd be great to create a new PR to extend the setParent function to accept null, given that the _parent field on the AbstractControl class now supports null (after #32671). Thank you.

@angular-automatic-lock-bot

Copy link
Copy Markdown

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 Nov 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants