0

I have the standard lightning-radio-group component on my LWC with below code:

<lightning-radio-group name="Selection" label="Is this your choice?"
                        required options={radioOptions} value={selectedVal} type="radio" data-id="radioGroup" onchange={handleRadioChange}>
                        </lightning-radio-group> 

JS:

handleRadioChange(event){
    try{
        this.selectedVal= event.target.value;
    } catch(e){
        console.error('handleRadioChange error: ', e);
    }
}

I can see the little red asterisk right before the label, but when I click on 'Continue' button on the page, I am not seeing any standard error validation. Am I missing something? Do I need to write some custom logic to capture what is being selected and throw custom errors if non-selected?

Reference: lightning-radio-group

Update: Upon trying the solution from sfdcfox, I am able to get the custom validation on lightning-radio-group work the first time, but the validation is completely getting skipped any further tries. Here is my attempt:

let isValid = [...this.template.querySelectorAll('[data-id="radioGroup"]')].reduce(
        function(validSoFar, field) {
            field.reportValidity();
            return validSoFar && field.checkValidity();
        }, true
    );
      if(!isValid) {
        //this.reportError(); // Whatever you want to do here
        //console.log('isValid INSIDE: ' +isValid);
        isValid = !isValid; //Tried to revert the value, still didn't work. Validation didn't fire after the first try. 
        return;
    } 

1 Answer 1

1

In a flow, you need a validate function, as described in the documentation:

@api validate() {
  const isValid = [...this.template.querySelectorAll('.input')].reduce(
    function(validSoFar, field) {
      field.reportValidity();
      return validSoFar && field.checkValidity()
    }, true
  );
  if(isValid) {
    return { isValid: true };
  } else {
    return { isValid: false, errorMessage: 'Check all required fields.' };
  }
}

If not using a flow, you need to handle validation on the button's click handler:

handleContinueButtonClick(event) {
  const isValid = [...this.template.querySelectorAll('.input')].reduce(
    function(validSoFar, field) {
      field.reportValidity();
      return validSoFar && field.checkValidity()
    }, true
  );
  if(!isValid) {
    this.reportError(); // Whatever you want to do here
    return;
  }
  this.continueToNext(); // Ditto
}
6
  • I am using a Modal window (instead of a Flow). Sure, I will try your approach and get back to you.
    – SatyaV
    Commented Sep 20, 2023 at 18:14
  • the above solution is working only for the first time. When I 'cancel' (or exit) the modal, and try to click continue without selecting any radio buttons, I am able to bypass the validation rule. Am I missing something?
    – SatyaV
    Commented Sep 20, 2023 at 20:44
  • @ArnoldJr. I'd have to build it out and test. It should work all the time. I'll write something up when I get a moment.
    – sfdcfox
    Commented Sep 20, 2023 at 23:17
  • I am looking forward to it.
    – SatyaV
    Commented Sep 21, 2023 at 13:16
  • 1
    @ArnoldJr. I doublechecked with lightning-radio-group, it works just fine. Here's the link to the repo. This should be deployable to any org, just add the quick action to your page layout, and you're good to go. The actual problem is likely in the code you haven't shared. If you want, we can take this offline and discuss further if you have code you can't publicly share. Contact info is in my bio.
    – sfdcfox
    Commented Sep 21, 2023 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .