Repository navigation
feat(forms): warn on duplicate ngModel name bindings within the same form - #68551
swseverance wants to merge 1 commit into
Conversation
TLDRI've given it some more thought, and perhaps the angular team should consider marking #13993 as "won't fix" and closing this PR. What this PR doesThis PR adds a dev-mode console warning (NG01354) when two or more <!-- Accidental duplication — the intended target -->
<form>
<input ngModel type="text" name="first">
<input ngModel type="text" name="first"> <!-- typo: should be "last" -->
</form>Because radio buttons intentionally share a The problem is that, in my humble opinion, we cannot reliably also exempt third party radio controls. The detection heuristic is a bit unsoundThe PR exempts radio controls by checking whether the conflicting controls both use PrimeNG — one of the most widely used Angular UI libraries — is a concrete example. Its <!-- PrimeNG — warns despite being correct usage -->
<form>
<p-radiobutton name="demo" value="option1" [(ngModel)]="selected" inputId="opt1" />
<p-radiobutton name="demo" value="option2" [(ngModel)]="selected" inputId="opt2" />
</form>Angular Material and ng-zorro avoid the warning because they wrap radio buttons in a group component with a single There's no fix available to the end userWhen a PrimeNG user (and perhaps other third party library users) sees this warning after upgrading Angular, what are they supposed to do? Their template is correct. They can't change PrimeNG's internals, and there's no way to suppress the warning. The only real fix requires a new public APIOne potential workaround would be to inspect the host element's DOM — if a descendant The robust solution would be for Angular to define a new interface or injection token — something like a For these reasons, I'm thinking that implementing a fix for #13993 might actually cause a problem bigger than #13993 itself. Thanks for reading this long comment! |
|
Do you mind rebasing ? |
…form Adds a dev-mode console warning (NG01354) when multiple ngModel directives are registered with the same name within the same form or ngModelGroup container. Duplicate names silently overwrite each other's values, leading to unpredictable behavior that is difficult to debug. Radio inputs sharing the same name are exempted, as that is the expected usage for radio button groups. Closes #13993
8dcf868 to
2f95bf5
Compare
|
@JeanMeche I have rebased. However, I still have the same concerns detailed in my previous comment. |
|
Yeah, you're right that makes perfect sense. |
|
This pull request has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Adds a dev-mode console warning (NG01354) when multiple ngModel directives are registered with the same name within the same form or ngModelGroup container. Duplicate names silently overwrite each other's values, leading to unpredictable behavior that is difficult to debug.
Radio inputs sharing the same name are exempted, as that is the expected usage for radio button groups.
Closes #13993
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?
Template driven forms users may unintentionally use duplicate name attributes. Here's an example of unintentional duplication of the
name="first"attribute.Unexpected behavior can occur when this is the case. In particular the value of
firstproperty withinf.valuedepends on which input data was entered into last.Issue Number: #13993
What is the new behavior?
In dev mode a warning is now logged to the console when using template driven forms if duplicate name attributes are detected within the same form or ngModelGroup
Does this PR introduce a breaking change?
Other information
Elements of the prior PR were taken into consideration when making this PR.