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
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4 

Turn a field required lightning component

Hello, 

I have a lightning component with two fields and I need turn one OR other required.
EX: i need put value in field A or field b. Can anyone help me?
 
Best Answer chosen by Leticia Monteiro Freitas 4
Raj VakatiRaj Vakati
As you need to validate OR condition. i will suggest to use the Javascript validation at the controller 
 
var inputCmp = component.find("inputCmp");
        var value = inputCmp.get("v.value");

        // is input numeric?
        if (isNaN(value)) {
            inputCmp.set("v.errors", [{message:"Input not a number: " + value}]);
        } else {
            inputCmp.set("v.errors", null);
        }
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_validate_fields.htm
 

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Leticia,

Greetings to you!

You can use required="true" if you want to make any field required in the Lightning component.

Below is the sample code which I have tested in my org and it is working fine.
 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name = "Acc" type="Account" default="{'sobjectType' : 'Account'}"/>
    <aura:attribute name="RatingPick" type="list" default="Hot, Warm, Cold" />
    
    <lightning:layoutItem class="slds-p-around--medium" size="6">
        <lightning:input aura:id="fieldId1"
                         required="true"
                         label="Name"
                         name="name"
                         value="{!v.Acc.Name}"/>
    </lightning:layoutItem>   
    
    <lightning:layoutItem class="slds-p-around--medium" size="6">
        <lightning:select aura:id="fieldId2"
                          required="true"
                          name="rating"
                          label="Rating"
                          value="{!v.Acc.rating}">
            <option value = "">Select</option>
            <aura:iteration items="{!v.RatingPick}" var="per">
            	<option value="{!per}" text="{!per}" />  
        	</aura:iteration>   
        </lightning:select>
    </lightning:layoutItem>   
</aura:component>

Also, please refer to below link for more information on Validating fields:

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_validate_fields.htm

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Raj VakatiRaj Vakati
As you need to validate OR condition. i will suggest to use the Javascript validation at the controller 
 
var inputCmp = component.find("inputCmp");
        var value = inputCmp.get("v.value");

        // is input numeric?
        if (isNaN(value)) {
            inputCmp.set("v.errors", [{message:"Input not a number: " + value}]);
        } else {
            inputCmp.set("v.errors", null);
        }
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_validate_fields.htm
 
This was selected as the best answer