components icon indicating copy to clipboard operation
components copied to clipboard

Radios should have separate unique selection in different form containers

Open dmitrysteblyuk opened this issue 9 years ago • 10 comments

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

dmitrysteblyuk avatar Jul 23 '17 16:07 dmitrysteblyuk

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

grizzm0 avatar Jul 23 '17 22:07 grizzm0

@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

dmitrysteblyuk avatar Jul 24 '17 04:07 dmitrysteblyuk

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.

crisbeto avatar Jul 24 '17 09:07 crisbeto

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);

dmitrysteblyuk avatar Jul 24 '17 20:07 dmitrysteblyuk

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).

jelbourn avatar Jul 25 '17 17:07 jelbourn

@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

grizzm0 avatar Jul 25 '17 17:07 grizzm0

@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 https://github.com/angular/material2/issues/5983#issuecomment-317540719 how it can be done.

dmitrysteblyuk avatar Jul 25 '17 17:07 dmitrysteblyuk

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.

jelbourn avatar Jul 25 '17 18:07 jelbourn

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 :-)

eliasre avatar Aug 03 '17 14:08 eliasre

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

DmitryEfimenko avatar Oct 07 '23 18:10 DmitryEfimenko