Skip to content

Radios should have separate unique selection in different form containers #5983

Open

Description

@dmitrysteblyuk

Bug, feature request, or proposal:

Bug

What is the expected behavior?

Radio group controls with the same name should not correlate with each other if they belong to different forms.

What is the current behavior?

If you change value in one radio group, then the value will be unset for all the other radio groups with the same name in other forms

What are the steps to reproduce?

<form>
  <md-radio-group ngModel="Winter" name="season">
    <md-radio-button *ngFor="let season of seasons" [value]="season">
      {{season}}
    </md-radio-button>
  </md-radio-group>
</form>

Second form:

<form>
  <md-radio-group ngModel="Summer" name="season">
    <md-radio-button *ngFor="let season of seasons" [value]="season">
      {{season}}
    </md-radio-button>
  </md-radio-group>
</form>

Providing a Plunker (or similar) is the best way to get the team to see your issue.
Plunker template: https://plnkr.co/edit/6xzFen4ayfMIv9JnYgW0?p=preview

What is the use-case or motivation for changing an existing behavior?

Currently it's impossible to implement a list (of similar items) if its items contain forms.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

@angular/cli: 1.2.0
node: 8.0.0
os: win32 x64
@angular/animations: 4.3.1
@angular/cdk: 2.0.0-beta.8
@angular/common: 4.3.1
@angular/compiler: 4.3.1
@angular/core: 4.3.1
@angular/forms: 4.3.1
@angular/http: 4.3.1
@angular/material: 2.0.0-beta.8
@angular/platform-browser: 4.3.1
@angular/platform-browser-dynamic: 4.3.1
@angular/platform-server: 4.3.1
@angular/router: 4.3.1
@angular/cli: 1.2.0
@angular/compiler-cli: 4.3.1
@angular/language-service: 4.3.1

Activity

  1. grizzm0 commented on Jul 23, 2017

    @grizzm0
    Contributor

    This is a limitation of Angular itself and not related to material2.
    Any input needs an unique name. See: https://angular.io/guide/forms#ngForm

  2. dmitrysteblyuk commented on Jul 24, 2017

    @dmitrysteblyuk
    Author

    @grizzm0 not true, same name works for other controls (I checked text inputs and checkboxes). I used ng-bootstrap before and radio-group also worked fine there. I also believe that the values in other controls are being unset only visually (and still present in inner object form.value or control.value): https://plnkr.co/edit/6xzFen4ayfMIv9JnYgW0?p=preview

  3. crisbeto commented on Jul 24, 2017

    @crisbeto
    Member

    FWIW, what is going on is that the name from the radio group gets passed down directly to the underlying input[type="radio"] that is inside each radio button, which is why things end up unchecking each other. We could add a prefix to the name before it is added, which will ensure that they can't interfere. I'm not completely sure how to handle cases where there are multiple of these groups within the same form.

  4. dmitrysteblyuk commented on Jul 24, 2017

    @dmitrysteblyuk
    Author

    So every radio group component (and some other components - accordion, button-toggle etc) listens to a single service UniqueSelectionDispatcher. Which makes it necessary for every form control to have a unique name attribute. Even if they belong to different forms or different components for that matter.

     this._removeUniqueSelectionListener =
          _radioDispatcher.listen((id: string, name: string) => {
            if (id != this.id && name == this.name) {
              this.checked = false;
            }
          });
    

    Suggested solution: add to UniqueSelectionDispatcher.notify/listen a third argument parentId and make sure that it is the same before applying the changes.
    And why to have specific arguments at all? It's just a class with two methods: notify and listen. Make it generic and with one argument. Then each component can specify which argument it would be.

    For example:

    constructor(
      private _radioDispatcher: UniqueSelectionDispatcher<MdRadioButton>
    ) {
    this._radioDispatcher.listen(radioButton => {
      if (this !== radioButton && this.radioGroup && this.radioGroup === radioButton.radioGroup) {
        this.checked = false;
      }
    });
    }
    ....
    // when checked:
    this._radioDispatcher.notify(this);
    
  5. jelbourn commented on Jul 25, 2017

    @jelbourn
    Contributor

    This is working as intended. The radio buttons follow the behavior of the native <input type="radio"> where the unique selection is mapped to the name property. For independent radio groups you can either specify a different name or omit the name completely (as the component will generate a unique name is it is unset).

  6. grizzm0 commented on Jul 25, 2017

    @grizzm0
    Contributor

    @jelbourn Radio groups does not bleed through the <form> tags natively which the plnkr in the original post does.

    See: http://plnkr.co/edit/zsaEzuYBohgm0bc7yzav?p=preview

  7. dmitrysteblyuk commented on Jul 25, 2017

    @dmitrysteblyuk
    Author

    @jelbourn so if I want to have a form with a radio group in a component then it is absolutely necessarily to set a specific prefix to a name and then strip it before sending data to server (otherwise this component is not reusable - if you have more than one such component on a page, then radio groups will not be working). Choosing to follow this behavior seems very inconvenient to me.
    And what's the real need to pass down the name to radio inputs at all? For example, in ng-bootstrap it is implemented without such behavior and without this bug. I suggested here #5983 (comment) how it can be done.

  8. jelbourn commented on Jul 25, 2017

    @jelbourn
    Contributor

    Ah, I wasn't aware the native radios had special behavior inside of a <form>. Ours should do the same, then.

    We use a native radio underneath for a11y reasons.

  9. changed the title [-]Radio group controls with the same name not working[/-] [+]Radios should have separate unique selection in different form containers[/+] on Jul 25, 2017
  10. added
    P4
    on Jul 25, 2017
  11. eliasre commented on Aug 3, 2017

    @eliasre

    I can see that this is labeled a minor issue, but I hope it gets attention.
    I can't see any elegant workarounds to this issue (don't particularly want to rename values before submitting to server) and i dont want to use native radios...

    (Hope I dont come across as to whiny or nagging).
    Keep up the good work on a beautiful framework :-)

  12. added
    help wanted
    on Aug 26, 2021
  13. DmitryEfimenko commented on Oct 7, 2023

    @DmitryEfimenko

    There was a PR for this issue, that got closed. What happened?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4area: material/radiohelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions