Skip to content

feat(forms): add error when adding control with dupe name to form - #46994

Closed
GrizzlyEnglish wants to merge 14 commits into
angular:mainfrom
GrizzlyEnglish:renglish/feat-dupe-name
Closed

GrizzlyEnglish wants to merge 14 commits into
angular:mainfrom
GrizzlyEnglish:renglish/feat-dupe-name

Conversation

@GrizzlyEnglish

@GrizzlyEnglish GrizzlyEnglish commented Jul 30, 2022 •

Copy link
Copy Markdown

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • 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?

Currently if a user adds controls to a form that share the same name, their could be unintended consequence due the models setting the controls by name in the form group and having no way to differentiate.

Issue Number: #13993

What is the new behavior?

When a control with a duplicate name is added to a form a runtime error is thrown to notify the user of the possibility of unintended consequences.

Does this PR introduce a breaking change?

  • Yes
  • No

I don't believe this would be a breaking change - since surely no one has existing apps with duplicate names.

Other information

I tried avoiding adding any parameters or anything to existing methods in order to make as small of an impact as possible; but, due to the promises and the use case of ngFor renaming controls as they are being deleted I had to add the changes to the control functions in order to determine if it was a rename or if the control was being added.

@pullapprove
pullapprove Bot requested review from atscott and dylhunn July 30, 2022 03:53
@GrizzlyEnglish
GrizzlyEnglish force-pushed the renglish/feat-dupe-name branch from 44e23d4 to e1dc78e Compare July 30, 2022 04:25
@GrizzlyEnglish

Copy link
Copy Markdown
Author

My bad on the failed tests, not really sure what is happening 'cause if I run them, they pass.

image

And I can forcefully make these tests fail by changing the expected header of the forms app

image

I am absolutely baffled currently, so my bad for having a PR with failed test 'cause like I said they work on my box :D. Probably due to environment more than anything.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@dylhunn At a glance, this change looks like it would be hugely breaking by adding a new required parameter to an existing function in the public API.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is fair. I think I can refactor this in a way to add a function to the Form interface that will add the control with the validation and if the end user calls addControl it will assume the changes are null - cant image the end user adding a control for a name change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should move this logic to the ngOnChanges hook on the NgModel class instead and guard it with ngDevMode check (if (ngDevMode) { ... the logic goes here ... }), so that it's tree-shaken away in prod mode.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the insight. Those are great ideas. I had wanted to put it in the ngOnChanges, but due to the add/remove of the controls being promises I wasn't sure of the best way to handle validating the current state of controls is up to date. But I will look into. Hope it's cool I leave this PR open while I work through this refactor. Thanks!

@AndrewKushnir AndrewKushnir Aug 2, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@GrizzlyEnglish sure, no problem. I will mark your PR as "draft" for now to indicate that further changes are expected. Feel free to move it back to "ready for review" via GitHub UI once the implementation is updated.

Regarding the implementation itself, it looks like you may need to get a hold of the _directives private field (you can remove "private" modifier and add /* @internal */ above it, so that it's accessible within the Forms package, but doesn't leak to the public API). Having that list available, the rest of the logic should work inside the ngOnChanges hook.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Awesome. Appreciate you @AndrewKushnir. I was afraid to change the private 😆

Comment thread goldens/public-api/forms/errors.md Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should move this logic to the ngOnChanges hook on the NgModel class instead and guard it with ngDevMode check (if (ngDevMode) { ... the logic goes here ... }), so that it's tree-shaken away in prod mode.

@AndrewKushnir AndrewKushnir added action: review The PR is still awaiting reviews from at least one requested reviewer area: forms labels Aug 1, 2022
@ngbot ngbot Bot modified the milestone: Backlog Aug 1, 2022
@AndrewKushnir AndrewKushnir added forms: ngModel target: minor This PR is targeted for the next minor release labels Aug 1, 2022
@AndrewKushnir
AndrewKushnir marked this pull request as draft August 2, 2022 01:07
@dylhunn
dylhunn marked this pull request as ready for review August 4, 2022 02:58
@dylhunn

dylhunn commented Aug 4, 2022

Copy link
Copy Markdown
Contributor

I have reopened as requested in #13993. Would you like me to review this as-is? @GrizzlyEnglish

@GrizzlyEnglish

Copy link
Copy Markdown
Author

@dylhunn Yes please! Thanks!

@dylhunn

dylhunn commented Aug 4, 2022 •

Copy link
Copy Markdown
Contributor

Great. My main concern on first glance is making sure this a non-breaking change. I'll get back to you soon with more specific comments.

@dylhunn dylhunn left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@GrizzlyEnglish This looks good to me, thanks for contributing! I have a couple super nitpicky comments below.

@AndrewKushnir It looks like all your comments have been addressed; mind giving this one more pass in case I missed something? Also do you know why pullapprove isn't requiring public-api reviews? It does touch a golden file. (Edit: it was just acting flaky, which is not new...)

reviewed-for: fw-forms, public-api

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This import is no longer needed, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks nope, removed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

to setup

Nit: add period

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Dropped using a promise in favor of ngZone, so this was removed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

registered but given ngFor

Nit: "registered. Given ngFor..."

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed

@AndrewKushnir AndrewKushnir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@GrizzlyEnglish thanks for updating the PR. I've left some comments. Please let me know if any additional information is needed. Thank you.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Due to the ZoneJS logic, the Promise.resolve would trigger a new round of change detection. I think a better approach would be to use ngZone.onStable as shown here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note: we'd probably need to maintain a subscription as well and unsubscribe before subscribing again (and also unsubscribe in ngOnDestroy).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@AndrewKushnir Thanks got this updated to utilize ngZone rather than a promise. With a empty subscription on creation and removal on detroy

Comment on lines 237 to 238

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's merge both if statements to avoid an extra space (indent) for the entire block:

Suggested change
if (ngDevMode) {
if (this.formDirective) {
if (ngDevMode && this.formDirective) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Merged

Comment on lines 247 to 248

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would be great to use !== and === here instead of != and ==.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, you can probably use changes[name]?.isFirstChange() here (if I understand correctly, the intention is to invoke the logic only the first time name is set).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah isFirstChange much better didn't notice it before. Thanks got this updated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change looks unnecessary now.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We've discussed this offline with @dylhunn and the proposal is to replace this throw with console.warn instead to avoid a breaking change.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated to a warn, and updated tests to match.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
this.formDirective._directives.forEach((d: NgModel) => {
this.formDirective._directives.forEach((directive: NgModel) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's add a comment into the code to describe why we exclude the RadioControlValueAccessor from this check.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added a comment in regards to the radio control needing duped names.

Comment on lines 243 to 244

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure I fully understand this sentence. I think one of the main reasons we delay the check is to make sure that the data structures are updated and the update happens in NgForm.addControl in a Promise. The NgZone.onStable should help delay the check in this case as well (instead of adding a new Promise). it'd be great to update the comment here to explain this as well (so there is a sufficient context in the future on why we have a delayed operation here).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the feedback. Yeah, my comment probably wasn't worded the best. Just wanted the dupe check to be placed on the end of the event loop in order to make sure it was after the completion of NgForm.addControl. Appreciate this and your other comments - will dig into understanding and implementing. Thanks!

@pullapprove
pullapprove Bot removed the request for review from atscott August 12, 2022 02:43
@GrizzlyEnglish
GrizzlyEnglish force-pushed the renglish/feat-dupe-name branch from 7bc0aec to 307b67f Compare August 12, 2022 02:46
@GrizzlyEnglish

Copy link
Copy Markdown
Author

@dylhunn @AndrewKushnir Apologies for the spam, and the delay. Got this updated based on feedback. Moved from using a promise to ngZone and an exception to console.warn. In doing so I had to update the tests as well. The only downside of this update was it was now triggering dupe names on everything; since, the ngZone triggers after and the directive exists in the collection. Just added a simple check to make sure it wasn't the same control.

Let me know if you see anything else. I appreciate the help!

@dylhunn

dylhunn commented Aug 16, 2022

Copy link
Copy Markdown
Contributor

@GrizzlyEnglish Andrew has gone on vacation for the next few weeks. Let me re-review this and find a replacement second approver.

@GrizzlyEnglish

Copy link
Copy Markdown
Author

@dylhunn I am in no rush whatso ever if you choose to wait, I will work whatever bits needed later on. I just would like to try another ticket after this one is completed haha. Thanks!

@dylhunn dylhunn Aug 18, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@GrizzlyEnglish Your changes look great overall!

However, this line is causing some code to break in the google3 presubmit. In particular:

TypeError: Cannot read properties of undefined (reading 'forEach')

    at Object.next (forms/src/directives/ng_model.ts?l=258)

I haven't dug into this yet -- just wanted to let you know it seems that _directives is not always set.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Oh, my bad. I only focused on tests passing - and it seems I lack access to google3 - so it didn't occur to me to check the results. I am going to YOLO a null check on directives and just see if it fixes it hope that's good.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's probably good enough, although I would like to at least understand why. And indeed you don't have access to google3, so I'd need to do a bit of digging.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah understood. If there is anything I can do let me know. I can always roll back the null check. Appreciate all the help!

@dylhunn

dylhunn commented Aug 18, 2022 •

Copy link
Copy Markdown
Contributor

The above issue is really surprising, because the _directives set is immediately initialized with new Set().

Maybe the initializer isn't running for some reason. All the stack traces look like this:

TypeError: Cannot read properties of undefined (reading 'forEach')

at Object.next (forms/src/directives/ng_model.ts?l=258
at SafeSubscriber.__tryOrUnsub (rxjs/src/internal/Subscriber.ts?l=268
at SafeSubscriber.next (rxjs/src/internal/Subscriber.ts?l=210
at Subscriber._next (rxjs/src/internal/Subscriber.ts?l=153
at Subscriber.next (rxjs/src/internal/Subscriber.ts?l=113
at Subject.next (rxjs/src/internal/Subject.ts?l=75
at EventEmitter_.emit (core/src/event_emitter.ts?l=114
at core/src/zone/ng_zone.ts?l=341
at _ZoneDelegate.invoke (zone.js/lib/zone.ts?l=1145
at FakeAsyncTestZoneSpec.onInvoke (zone.js/lib/zone-spec/fake-async-test.ts?l=674
at ProxyZoneSpec.onInvoke (zone.js/lib/zone-spec/proxy.ts?l=134
at _ZoneDelegate.invoke (zone.js/lib/zone.ts?l=1142
at Zone.run (zone.js/lib/zone.ts?l=825
at NgZone.runOutsideAngular (core/src/zone/ng_zone.ts?l=244
at checkStable (core/src/zone/ng_zone.ts?l=341

The common stack frames to all presubmit failures start at FakeAsyncTestZoneSpec.onInvoke, and nearer.

@GrizzlyEnglish

Copy link
Copy Markdown
Author

@dylhunn Sorry to be a pain on this. I am not sure how to fix this. My most recent commit a14f30838a2606d73098635ed3f30828fd25a640 is saying that it is too large; but it is the required symbols.

@jessicajaniuk

Copy link
Copy Markdown
Contributor

@GrizzlyEnglish You should be able to look at the test failure and update the golden file with the new size. The file is in the root of the repo at goldens/size-tracking/integration-payloads.json. Update the block for forms > uncompressed with the new values. Once you do that, we'll get pinged for size tracking reviews.

@angular-robot angular-robot Bot added the feature Label used to distinguish feature request from other issues label Oct 7, 2022
@GrizzlyEnglish

Copy link
Copy Markdown
Author

@jessicajaniuk Thank you!

@GrizzlyEnglish
GrizzlyEnglish force-pushed the renglish/feat-dupe-name branch from 77a3fe1 to 404190e Compare October 7, 2022 18:23
@jessicajaniuk

Copy link
Copy Markdown
Contributor

@GrizzlyEnglish looks like your fixup commit updated the size to the expected value and not the actual value. The failing size test shows the main uncompressed for forms actual value as 158276. Setting it to that should get it passing.

@GrizzlyEnglish

Copy link
Copy Markdown
Author

@jessicajaniuk Ah my bad. Working and working on this not a good combo :D

@dylhunn

dylhunn commented Oct 10, 2022

Copy link
Copy Markdown
Contributor

I or Andrew will look at this shortly, but for now let's target v15.1.

@JeanMeche

Copy link
Copy Markdown
Member

Do you mind rebasing your PR and fix the conflicts ?

@thePunderWoman
thePunderWoman removed the request for review from jessicajaniuk April 4, 2024 20:38
@thePunderWoman

Copy link
Copy Markdown
Contributor

Since we've seen no movement on this PR, I'm going to close this. If you'd like to see these changes land, feel free to open a new PR.

@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 Jun 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer area: forms feature Label used to distinguish feature request from other issues forms: ngModel target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants