function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Neil JamisonNeil Jamison 

Make lightning:inputRichText required

Hello Salesforce developer community! I'm working on my second lightning component and I feel like I'm 95% there but stuck on one thing. Apologies if the answer to this is already posted somewhere or it is in the developer documentation. I'm not sure how to search to ask the right question. 

Scenario: I'm creating a component that will be used in a flow (implements="lightning:availableForFlowScreens"). Part of the component is a Rich Text field and I want to be able to make that rich text field required. 

<div>
     <lightning:inputRichText value="{!v.myVal}" placeholder="Enter text with formatting" messageWhenBadInput="{!v.errorMessage}" valid="{!v.validity}"/>
</div>
This much works but I want to be able to make my field required. In fact as a test, I have temporarily added a button that checks that the field has something in it:
<lightning:button name="validate" label="Validate" onclick="{!c.validate}"/>
validate: function(cmp,event,helper) {
        if(!cmp.get("v.myVal")){
            cmp.set("v.validity", false);
        }
        else{
            cmp.set("v.validity", true);
        }
    }

However, I don't want the button. I want to check that the field is not empty when the user clicks the Next button on the flow screen. 

I know I can do both of the following solutions but I would prefer a more elegant solution:

1. I could write code with a replacement set of navigation buttons. This would allow me to override the functionality of the Next button. I want to use the standard navigation if possible so I'm hoping this is not required.

2. I could pass the result of the lightning:inputRichText to the flow and make the flow do the validation and pass the user back to the same screen with an error message. However I want my component to be self-contained so this is not the ideal solution because it requires work to be done in the flow each time the component is used.

Is there any way to get flow to recognize that a component on the screen is in an invalid state using standard navigation? In other words, I want the lightning:inputRichText to work like any other required field would work where when you click next, it throws an error if the field is not filled in.
 

Alain CabonAlain Cabon
Hi,

More simply perhaps by using the javascript default mechanism of true/false/null:
 
<lightning:inputRichText value="{!v.myVal}" valid="{!v.myVal}" placeholder="Enter text with formatting"  />

The component has a red border by default (invalid) until you enter the first character (valid).

You probably already test this option.
S_RathS_Rath
Hi,

You can give the aura:id to your element and can check the validity of any of the element by fetching the component and adding the below checks. You can show the toast and alert as per the requirement. You can put the below code in the controller and can modify it. 

if( !component.find('auraId').get("v.validity").valid ) {
            component.find('auraId').showHelpMessageIfInvalid();
            alert('Please fill the required field');
}

Hope, this will help you !!!

Thanks