Repository navigation
feat(forms): add error when adding control with dupe name to form - #46994
GrizzlyEnglish wants to merge 14 commits into
Conversation
44e23d4 to
e1dc78e
Compare
|
My bad on the failed tests, not really sure what is happening 'cause if I run them, they pass. And I can forcefully make these tests fail by changing the expected header of the forms app 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. |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
Awesome. Appreciate you @AndrewKushnir. I was afraid to change the private 😆
There was a problem hiding this comment.
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.
|
I have reopened as requested in #13993. Would you like me to review this as-is? @GrizzlyEnglish |
|
@dylhunn Yes please! Thanks! |
|
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. |
There was a problem hiding this comment.
@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
There was a problem hiding this comment.
This import is no longer needed, right?
There was a problem hiding this comment.
Dropped using a promise in favor of ngZone, so this was removed.
There was a problem hiding this comment.
registered but given ngFor
Nit: "registered. Given ngFor..."
AndrewKushnir
left a comment
There was a problem hiding this comment.
@GrizzlyEnglish thanks for updating the PR. I've left some comments. Please let me know if any additional information is needed. Thank you.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Note: we'd probably need to maintain a subscription as well and unsubscribe before subscribing again (and also unsubscribe in ngOnDestroy).
There was a problem hiding this comment.
@AndrewKushnir Thanks got this updated to utilize ngZone rather than a promise. With a empty subscription on creation and removal on detroy
There was a problem hiding this comment.
Let's merge both if statements to avoid an extra space (indent) for the entire block:
| if (ngDevMode) { | |
| if (this.formDirective) { | |
| if (ngDevMode && this.formDirective) { |
There was a problem hiding this comment.
Would be great to use !== and === here instead of != and ==.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Yeah isFirstChange much better didn't notice it before. Thanks got this updated.
There was a problem hiding this comment.
This change looks unnecessary now.
There was a problem hiding this comment.
We've discussed this offline with @dylhunn and the proposal is to replace this throw with console.warn instead to avoid a breaking change.
There was a problem hiding this comment.
Updated to a warn, and updated tests to match.
There was a problem hiding this comment.
| this.formDirective._directives.forEach((d: NgModel) => { | |
| this.formDirective._directives.forEach((directive: NgModel) => { |
There was a problem hiding this comment.
Let's add a comment into the code to describe why we exclude the RadioControlValueAccessor from this check.
There was a problem hiding this comment.
Added a comment in regards to the radio control needing duped names.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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!
7bc0aec to
307b67f
Compare
|
@dylhunn @AndrewKushnir Apologies for the spam, and the delay. Got this updated based on feedback. Moved from using a promise to Let me know if you see anything else. I appreciate the help! |
|
@GrizzlyEnglish Andrew has gone on vacation for the next few weeks. Let me re-review this and find a replacement second approver. |
|
@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! |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ah understood. If there is anything I can do let me know. I can always roll back the null check. Appreciate all the help!
|
The above issue is really surprising, because the Maybe the initializer isn't running for some reason. All the stack traces look like this: The common stack frames to all presubmit failures start at |
|
@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. |
|
@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 |
|
@jessicajaniuk Thank you! |
This commits adds an error when a form is generated with more than one control with the same name. Since there is no way to differentiate the controls with the same name an error is now thrown. This allows users to quickly see their mistake and fix it accordingly. fixes #199339
77a3fe1 to
404190e
Compare
|
@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. |
|
@jessicajaniuk Ah my bad. Working and working on this not a good combo :D |
|
I or Andrew will look at this shortly, but for now let's target v15.1. |
|
Do you mind rebasing your PR and fix the conflicts ? |
|
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. |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |


PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
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?
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.