Skip to content

feat(forms): warn on duplicate ngModel name bindings within the same form - #68551

Closed
swseverance wants to merge 1 commit into
angular:mainfrom
swseverance:fix/13993
Closed

swseverance wants to merge 1 commit into
angular:mainfrom
swseverance:fix/13993

Conversation

@swseverance

Copy link
Copy Markdown
Contributor

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?

  • 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.dev application / infrastructure changes
  • Other... Please describe:

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.

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <form #f="ngForm">
      <label for="first">First</label>
      <input ngModel type="text" id="first" name="first">
      
      <label for="last">Last</label>
      <input ngModel type="text" id="last" name="first">

      <div>
        {{ f.value | json }}
      </div>
    </form>
  `,
  imports: [FormsModule, JsonPipe]
})
export class App {}

Unexpected behavior can occur when this is the case. In particular the value of first property within f.value depends 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?

  • Yes
  • No

Other information

Elements of the prior PR were taken into consideration when making this PR.

@pullapprove
pullapprove Bot requested a review from JeanMeche May 4, 2026 18:54
@angular-robot angular-robot Bot added detected: feature PR contains a feature commit area: forms labels May 4, 2026
@ngbot ngbot Bot added this to the Backlog milestone May 4, 2026
@swseverance

Copy link
Copy Markdown
Contributor Author

TLDR

I'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 does

This PR adds a dev-mode console warning (NG01354) when two or more ngModel directives are registered with the same name within the same form or ngModelGroup. The motivation is legitimate: duplicate names silently overwrite each other's values, and the bug is notoriously hard to track down.

<!-- 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 name attribute, the PR exempts them — specifically by checking whether both conflicting controls use Angular's built-in RadioControlValueAccessor. If they do, no warning is issued.

The problem is that, in my humble opinion, we cannot reliably also exempt third party radio controls.

The detection heuristic is a bit unsound

The PR exempts radio controls by checking whether the conflicting controls both use RadioControlValueAccessor. That check is correct for native Angular radio inputs — but Angular's ControlValueAccessor interface can be implemented by third parties. A third party radio-like control may use a different accessor and it will NOT be exempted.

PrimeNG — one of the most widely used Angular UI libraries — is a concrete example. Its p-radiobutton component registers its own value accessor, not RadioControlValueAccessor. Valid, correct usage of the library would still trigger a warning in the console:

<!-- 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 ngModel, so no name collision appears at the ngModel level — but that's specific to how those libraries happen to be architected.

There's no fix available to the end user

When 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 API

One potential workaround would be to inspect the host element's DOM — if a descendant input[type="radio"] exists, treat it as radio-like and skip the warning. But this couples the forms module to the internal DOM structure of third-party components, which is fragile, and it wouldn't cover components that use role="radio" on non-input elements or shadow DOM. I don't think it's a realistic solution.

The robust solution would be for Angular to define a new interface or injection token — something like a RadioLikeControlValueAccessor marker — that third-party libraries could implement to signal that shared name attributes are intentional. That is a deliberate API design decision that would need ecosystem buy-in and migration time, and it can't really be bolted on alongside the warning itself.

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!

@JeanMeche

Copy link
Copy Markdown
Member

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
@swseverance

Copy link
Copy Markdown
Contributor Author

@JeanMeche I have rebased. However, I still have the same concerns detailed in my previous comment.

@JeanMeche

Copy link
Copy Markdown
Member

Yeah, you're right that makes perfect sense.

@angular-automatic-lock-bot

Copy link
Copy Markdown

This pull request 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 Oct 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: forms detected: feature PR contains a feature commit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Forms should throw error if two or more NgModels appear in form with the same name

2 participants