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
Atul Shendge 2Atul Shendge 2 

How to make a field mandatory using lightning component controller

Hi,
In my scenario, I have a field called question and it should be mandatory, If User missed to enter this field it should throw an error and I am trying this to acheive through Lightning component. 
Below is the component code which is working fine:
<lightning:layout>
        <lightning:layoutitem><p style="color:red;">*</p></lightning:layoutitem>&nbsp;
        <lightning:layoutitem>
            <tr><td>
                <div style="font-size:14px;color:black;"><B>Question 1 </B> </div>
                </td></tr>
            <p>
                <br/>
        What is the invention? What problem does the invention solve? What are the features of the invention and how do they solve the problem? Describe your implementation(s) of the invention using examples. (Note: you can upload drawings and other documents in Step 5.)
    </p>
            <tr><td> <lightning:inputRichText aura:id="TxtAnswer1" onblur="{!c.assignAnswer1}" value="{!v.QuestAnswer1}"/></td></tr>
        </lightning:layoutitem>
    </lightning:layout>
    <br/>

Here is the Controller and I think something needs to be added in this but I am not sure, Below is the Controller code:
{
    assignAnswer1 : function(component, event, helper) {
        var Answer1=component.find("TxtAnswer1").get("v.value");
        component.set("v.QuestAnswer1", Answer1);

Any help would be really appreciated.

Regards,
Atul Shendge
VinayVinay (Salesforce Developers) 
Hi Atul,

Update with below snippet.
 
<lightning:inputRichText aura:id="TxtAnswer1" required="true" onblur="{!c.assignAnswer1}" value="{!v.QuestAnswer1}"/>

Thanks,
Vinay Kumar​
Atul Shendge 2Atul Shendge 2
Hi Vinay,

Thanks for the reply. But I am facing below error.

User-added image
Agustin BAgustin B
Hi Atul, check this documentation, it will help you with your request:
https://developer.salesforce.com/docs/component-library/bundle/lightning-input-rich-text/documentation
If it solves your issue please mark this as correct, it may help others.
VinayVinay (Salesforce Developers) 
Hi Atul,

Error says you cannot use required attribute in input text.  You need to check for alternate ui: inputtext or implement custom JS functionality.

https://salesforce.stackexchange.com/questions/210911/make-a-lightning-uiinputtext-required

Thanks,
Vinay Kumar
David Zhu 🔥David Zhu 🔥
You may try this:
in HTML
<aura:attribute name='txtAnswer1Valid' type='Boolean' default='false'/>

<lightning:inputRichText aura:id="TxtAnswer1" onblur="{!c.assignAnswer1}" value="{!v.QuestAnswer1}" valid="{!v.txtAnswer1Valid}" />


assignAnswer1 : function(component, event, helper) {

        if (!component.get("v.TxtAnswer1"))
        {
            component.set("v.txtAnswer1Valid",false);
             return;
        } 
        else
        {
            component.set("v.txtAnswer1Valid",true);
        }


        var Answer1=component.find("TxtAnswer1").get("v.value");
        component.set("v.QuestAnswer1", Answer1);