I'm trying to understand how to add custom error messaging to the lightning:select base component.
I understand that I can use the validity object to check if there is bad data and set a respective custom message. What I am having trouble understanding is how to apply it to this scenario:
- I have a lightning:select component defined with the values "Red,Blue,Green".
- Because I want to force a selection, I've added "– None –" as the first value in the list.
- So I have "– None –,Red,Blue,Green"
How do I tell the base component that "– None –" is invalid and an error needs to be shown?
Thanks!
Best Answer
The magic happens when you combine the
required
,onchange
, andmessageWhenValueMissing
attributes together.The error message specified by
messageWhenValueMissing
is shown when the first option is selected. You can also pair this solution with theshowHelpMessageIfInvalid
method to trigger the error from your JS controller.Credits to @Eugene Kashida.