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.
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.
Property
Type
Description
label
string
The text that displays next to a checkbox.
value
string
The 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.
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 interaction8 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
Name
Description
Type
Default
Required
accesskey
Specifies a shortcut key to activate or focus an element.
String
body
The body of the component. In markup, this is everything in the body of the tag.
Aura.Component[]
class
A CSS class for the outer element, in addition to the component's base classes.
String
disabled
Set to true if the checkbox group is disabled. Checkbox selections can't be changed for a disabled checkbox group. This value defaults to false.
boolean
false
label
Text label for the checkbox group.
String
messageWhenValueMissing
Optional message displayed when no checkbox is selected and the required attribute is set to true.
String
name
The name of the checkbox group.
String
onblur
The action triggered when the element releases focus.
Aura.Action
onchange
The action triggered when a checkbox value changes.
Aura.Action
onfocus
The action triggered when the element receives focus.
Aura.Action
options
Array of label-value pairs for each checkbox.
List
required
Set to true if at least one checkbox must be selected. This value defaults to false.
boolean
false
tabindex
Specifies 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
title
Displays tooltip text when the mouse moves over the element.
String
value
The 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[]
variant
The 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.
String
standard
Methods
Name
Description
Argument Name
Argument Type
Argument Description
checkValidity
Indicates whether the checkbox group has any validity errors.
focus
Sets focus on the element.
reportValidity
Display error messages if the checkbox group is invalid.
setCustomValidity
Sets a custom error message to be displayed when the checkbox group input is submitted.
message
String
The string that describes the error. If message is an empty string, the error message is reset.
showHelpMessageIfInvalid
Displays error messages. The checkbox group is invalid if it fails at least one constraint validation and when checkValidity() returns false.