You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Radios should have separate unique selection in different form containers #5983
P4A relatively minor issue that is not relevant to core functionsarea: material/radiohelp wantedLabel noting an issue which the team is looking for contribution from the community to fix
@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
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.
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.
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 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.
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
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 :-)
P4A relatively minor issue that is not relevant to core functionsarea: material/radiohelp wantedLabel noting an issue which the team is looking for contribution from the community to fix
Activity
grizzm0 commentedon Jul 23, 2017
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
dmitrysteblyuk commentedon Jul 24, 2017
@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
crisbeto commentedon Jul 24, 2017
FWIW, what is going on is that the
namefrom the radio group gets passed down directly to the underlyinginput[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.dmitrysteblyuk commentedon Jul 24, 2017
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.
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:
jelbourn commentedon Jul 25, 2017
This is working as intended. The radio buttons follow the behavior of the native
<input type="radio">where the unique selection is mapped to thenameproperty. 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).grizzm0 commentedon Jul 25, 2017
@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
dmitrysteblyuk commentedon Jul 25, 2017
@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.
jelbourn commentedon Jul 25, 2017
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.
[-]Radio group controls with the same name not working[/-][+]Radios should have separate unique selection in different form containers[/+]eliasre commentedon Aug 3, 2017
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 :-)
DmitryEfimenko commentedon Oct 7, 2023
There was a PR for this issue, that got closed. What happened?