Skip to content

fix(material/radio): keep selections independent across forms - #34174

Open
sviat-barbutsa wants to merge 1 commit into
angular:mainfrom
sviat-barbutsa:fix/radio-form-ownership
Open

sviat-barbutsa wants to merge 1 commit into
angular:mainfrom
sviat-barbutsa:fix/radio-form-ownership

Conversation

@sviat-barbutsa

Copy link
Copy Markdown

Fixes #5983. Thanks to @tinayuangao for the earlier work in #10129.

Problem & Fix

When two <form> elements on the same page share a radio field name (e.g., choice), selecting an option in the second form unchecks the native radio in the first. Even though the first form's Angular model still holds "email", new FormData(firstForm).get('choice') ends up returning null. With this fix, both forms maintain their independent selections and submit email and post respectively.

  • Form-scoped selection: Material radios now scope their selection state to their nearest ancestor <form> (or the root of their DOM tree if placed outside a form). Buttons sharing the same name and scope still uncheck each other, including standalone mat-radio-button elements alongside a mat-radio-group.
  • Backward-compatible CDK dispatcher: Kept the existing UniqueSelectionDispatcher behavior intact so apps listening for a radio's ID and name continue receiving both. The new scope and listener filter are optional—calling notify(id, name) still broadcasts to all listeners.

Testing

  • Unit & regression tests: Added 4 Material radio tests and 3 CDK dispatcher tests. Both the initial-selection and click regressions fail on unmodified main in Chromium and Firefox.
  • Test suites & lint: All 178 Chromium component suites passed (8,632 tests), along with the 5 affected suites in Firefox (334 tests, including radio harness, collection, accordion, and menu). CDK/Material API checks, radio docs build, formatting, and repository lint all pass.
  • Cross-browser & SSR: Verified submitted form values via keyboard and pointer across Chrome, Edge, Opera, and Firefox on Windows, plus keyboard navigation in Safari 18.2 on macOS. Server-rendered HTML also preserves one checked input per form.
  • Screen readers (NVDA & VoiceOver): After selecting "Email A" in the first form and "Post B" in the second, returning focus to "Email A" in Chrome on Windows logged "Email A radio button checked" with NVDA (compared to "not checked" on main), while still reporting "Post B" as checked. Confirmed the same behavior with VoiceOver in Safari on macOS.
Scope radio selection to the owning form or DOM tree so selecting a value
in one form preserves the other form's submitted value. Keep unscoped
CDK selection notifications available to existing listeners.

Fixes angular#5983

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant