[SalesForce] lightning:radioGroup is allowing multi-select

I have a lightning:radioGroup that shows record types for an object:

<aura:attribute name="recordTypes" type="String[]"/>
<aura:attribute name="recordTypeValue" type="String"/>

<lightning:radioGroup options="{!v.recordTypes}" value="{!v.recordTypeValue}" required="true"/>

In my controller I populate the recordTypes list with unique label-value pairs.

The radio buttons and their labels show up nicely. However, suddenly they become multi-selectable:

  • select option1 > select option2 > select option1 (now both options are selected)

When I debug recordTypeValue I can see 2 Ids written into it. I am not seeing any attributes on the said component that allows me to enable/disable multi-selecting.

Best Answer

You've forgotten a required attribute. The radioGroup is grounded in HTML's <input type="radio" />, which uses a "name" attribute to determine groups. If you forget to specify the name, the options actually behave like independent radio groups. The following change is necessary for proper operation:

<lightning:radioGroup name="recordTypes" options="{!v.recordTypes}" value="{!v.recordTypeValue}" required="true" />

Where "name" should be unique to each lightning:radioGroup within the component.

Note: The documentation lists "name" as optional; this is clearly a developer/documentation oversight, because without a name, this component won't work as expected.

Related Topic