Checkbox Group

lightning:checkboxGroup

A checkbox group that enables selection of single or multiple options. This component requires API version 41.0 and later.

For Aura components only. For LWC development, use lightning-checkbox-group.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline

A lightning:checkboxGroup component represents a checkbox group that enables selection of single or multiple options.

If the required attribute is set to true, at least one checkbox must be selected. When a user interacts with the checkbox group and doesn’t make a selection, an error message is displayed. You can provide a custom error message using the messageWhenValueMissing attribute.

If the disabled attribute is set to true, checkbox selections can’t be changed.

This component implements styling from Checkbox in the Lightning Design System.

This example creates a checkbox group with two options and option1 is selected by default. At least one checkbox must be selected as the required attribute is true.

1<aura:component>
2  <aura:attribute
3    name="options"
4    type="List"
5    default="[
6    {'label': 'Ross', 'value': 'option1'},
7    {'label': 'Rachel', 'value': 'option2'},
8    ]"
9  />
10  <aura:attribute name="value" type="List" default="option1" />
11  <lightning:checkboxGroup
12    aura:id="mygroup"
13    name="checkboxGroup"
14    label="Checkbox Group"
15    options="{! v.options }"
16    value="{! v.value }"
17    onchange="{! c.handleChange }"
18    required="true"
19  />
20</aura:component>

You can check which values are selected by using cmp.find("mygroup").get("v.value"). To retrieve the values when a checkbox is selected or deselected, use the onchange event handler and call event.getParam("value").

1({
2  handleChange: function (cmp, event) {
3    var changeValue = event.getParam("value");
4    alert(changeValue);
5  },
6});

Creating Checkboxes 

To create checkboxes, pass in the following properties to the options attribute.

PropertyTypeDescription
labelstringThe text that displays next to a checkbox.
valuestringThe string that’s used to identify which checkbox is selected.

Input Validation 

Client-side input validation is available for this component. For example, an error message is displayed when the checkbox group is marked required and no option is selected. Note that a disabled checkbox group is always valid.

The validation occurs for the checkbox group, not for an individual checkbox. To override the default message “Complete this field” displayed when a selection on a checkbox group is required and no option is selected, use the messageWhenValueMissing attribute. This message is displayed when you remove focus from the checkbox group.

The validity attribute returns the ValidityState object, with the following supported properties.

  • valid: Returns true if the checkbox group meets all its validation constraints.
  • valueMissing: Returns true if the checkbox group is required but no checkbox is selected.

Other properties such as badInput are not supported.

This example creates a checkbox group that requires a selection and a button that checks validity when clicked.

1<aura:component>
2  <aura:attribute
3    name="options"
4    type="List"
5    default="[
6            {'label': 'Red', 'value': 'red'},
7            {'label': 'Green', 'value': 'green'},
8            {'label': 'Blue', 'value': 'blue'}                                                        
9            ]"
10  />
11  <aura:attribute name="value" type="List" default="" />
12  <aura:attribute
13    name="message"
14    type="String"
15    default="You haven't selected a color"
16  />
17
18  <lightning:checkboxGroup
19    aura:id="colors"
20    label="Select a color"
21    options="{! v.options }"
22    value="{! v.value }"
23    required="true"
24  />
25
26  <lightning:button label="Check validity" onclick="{!c.handleValidity}" />
27  <p>{!v.message}</p>
28</aura:component>

checkValidity() returns true if at least one checkbox is selected, or false if none is selected. Calling checkValidity() is equivalent to returning validity.valid on the checkbox group.

To programmatically display error messages on invalid fields, use the reportValidity() method.

1({
2  handleValidity: function (cmp, event) {
3    var checkboxValidity = cmp.find("colors").get("v.validity");
4    if (checkboxGroup.checkValidity()) {
5      cmp.set("v.message", "That's a great selection!");
6    } else {
7      // Shows the error immediately without user interaction
8      checkboxGroup.reportValidity();
9      cmp.set("v.message", "Select your favorite color and try again.");
10    }
11  },
12});

For custom validity error messages, display the message using setCustomValidity() and reportValidity(). setCustomValidity() overrides the error message you provide using the messageWhenValueMissing attribute. For more information, see the lightning:input documentation.

Usage Considerations 

lightning:checkboxGroup is useful for grouping a set of checkboxes. If you have a single checkbox, use lightning:input type="checkbox" instead.

Accessibility 

The checkbox group is nested in a fieldset element that contains a legend element. The legend contains the label value. The fieldset element enables grouping of related checkboxes to facilitate tabbing navigation and speech navigation for accessibility purposes. Similarly, the legend element improves accessibility by enabling a caption to be assigned to the fieldset.

Attributes 

NameDescriptionTypeDefaultRequired
accesskeySpecifies a shortcut key to activate or focus an element.String
bodyThe body of the component. In markup, this is everything in the body of the tag.Aura.Component[]
classA CSS class for the outer element, in addition to the component's base classes.String
disabledSet to true if the checkbox group is disabled. Checkbox selections can't be changed for a disabled checkbox group. This value defaults to false.booleanfalse
labelText label for the checkbox group.String
messageWhenValueMissingOptional message displayed when no checkbox is selected and the required attribute is set to true.String
nameThe name of the checkbox group.String
onblurThe action triggered when the element releases focus.Aura.Action
onchangeThe action triggered when a checkbox value changes.Aura.Action
onfocusThe action triggered when the element receives focus.Aura.Action
optionsArray of label-value pairs for each checkbox.List
requiredSet to true if at least one checkbox must be selected. This value defaults to false.booleanfalse
tabindexSpecifies the tab order of an element when the Tab key is used for navigating. The tabindex value can be set to 0 or -1. The default is 0, which means that the component is focusable and participates in sequential keyboard navigation. -1 means that the component is focusable but does not participate in keyboard navigation.Integer
titleDisplays tooltip text when the mouse moves over the element.String
valueThe list of selected checkboxes. Each array entry contains the value of a selected checkbox. The value of each checkbox is set in the options attribute.String[]
variantThe variant changes the appearance of the checkbox group. Accepted variants include standard, label-hidden, label-inline, and label-stacked. This value defaults to standard. Use label-hidden to hide the label but make it available to assistive technology. Use label-inline to horizontally align the label and checkbox group. Use label-stacked to place the label above the checkbox group.Stringstandard

Methods 

NameDescriptionArgument NameArgument TypeArgument Description
checkValidityIndicates whether the checkbox group has any validity errors.
focusSets focus on the element.
reportValidityDisplay error messages if the checkbox group is invalid.
setCustomValiditySets a custom error message to be displayed when the checkbox group input is submitted.messageStringThe string that describes the error. If message is an empty string, the error message is reset.
showHelpMessageIfInvalidDisplays error messages. The checkbox group is invalid if it fails at least one constraint validation and when checkValidity() returns false.