How to validate child component from parent component on click button using aura method in lightning component | how to validate child component from parent component in lightning component Salesforce

1,831 views

Hey guys, today in this post we are going to learn about how to validate child component from parent component on click button using aura method in Salesforce lightning component.

Files we used in this post example:-

eventApp.app Lightning Application It is used for call the component to preview on browser.

eventCmp.cmp

Lightning Component It is parent component of "eventValidationCmp.cmp".

eventCmpController.js

Lightning Component Controller It is used for click functionality that gets the value from child component to aura method.
eventValidationCmp.cmp Lightning Component It is a child component of "eventCmp.cmp" and hold aura method attribute with action method.
eventValidationCmpController.js Lightning Component Controller It is used for display validation error messages in lightning component.
eventValidationCmp.css Component Style CSS It is used for create custom stylish form validation error message.

Real time scenarios:- Create a custom and stylish form validation and validate child component from parent component using aura method in lightning component.

Final Output

Why Should You Schedule Meeting?

🎯 If You Are Facing Any Of These 6 Challenges. Schedule Meeting With Me.

  • Learn Salesforce Development
  • Career Confusion
  • No Interview Call
  • Low Salary
  • No Promotion/Growth
  • No Finding New Job Opportunity
  • Why you stucking from past so many years in same company?

 

display validation error messages in lightning component -- w3web.net

 

You can download file directly from github by Click Here.

 

Other related post that would you like to learn in Salesforce

 

 

Step 1:- Create Lightning Application : eventApp.app

From Developer Console >> File >> New >> Lightning Application : eventApp.app

eventApp.app [Component Application File]

  1.   <aura:application extends="force:slds">
  2.     <c:eventCmp/>
  3. </aura:application>

Step 2:- Create Lightning Component : eventCmp.cmp

From Developer Console >> File >> New >> Lightning Component

eventCmp.cmp [Lightning Component File]

  1.   <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">      
  2.     <c:eventValidationCmp aura:id="getEventId"/>        
  3.     <div class="slds-grid slds-wrap">   
  4.             <div class="slds-text-align--center slds-col slds-size_6-of-12 slds-m-bottom--medium">      
  5.                  <button class="slds-button slds-button--brand" onclick="{!c.eventValidation}">Submit</button>
  6.             </div>
  7.  
  8.  <br/><br/>
  9.    <!--Start RelatedTopics Section-->
  10. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  11.  
  12.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click  <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
  13.  
  14.             <br/><br/>
  15.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
  16.             <div style="display:block; overflow:hidden;"> 
  17.                 <div style="width: 50%; float:left; display:inline-block">
  18.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  19.                         <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  20.                         <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  21.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  22.                         <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  23.                         <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  24.                     </ul>
  25.             </div>
  26.  
  27.             <div style="width: 50%; float:left; display:inline-block">
  28.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  29.                         <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  30.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  31.                         <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  32.                         <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  33.                         <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
  34.                     </ul>
  35.                 </div>
  36.                <div style="clear:both;"></div> 
  37.                <br/>
  38.                 <div class="youtubeIcon">
  39.                     <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
  40.                 </div>
  41.     </div>
  42.  
  43. </div>
  44.  
  45.   <!--End RelatedTopics Section-->
  46.  
  47.         </div>
  48. </aura:component>

 

Step 3:- Create Lightning Component : eventCmpController.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

eventCmpController.j [JavaScript Controller]

  1.    ({
  2. 	eventValidation : function(component, event, helper) {
  3.         var firstNameId1 = component.find('firstNameId');         
  4.         component.find('getEventId').validationDR(firstNameId1);
  5.  
  6. 	},
  7.  
  8. })

 

Step 4:- Create Lightning Component : eventValidationCmp.cmp

From Developer Console >> File >> New >> Lightning Component

eventValidationCmp.cmp [Lightning Component File]

  1.    <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
  2.     <aura:attribute name="fieldVal" type="string"/>
  3.     <aura:method name="validationDR" action="{!c.eventInpValidation}">      
  4.     </aura:method>
  5.  
  6.     <div class="slds slds-p-horizontal--medium">
  7.         <div class="slds-grid slds-wrap">   
  8.             <div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">      
  9.                 <div class="slds-form-element">
  10.                     <label class="slds-form-element__label">Name</label>
  11.                     <div class="slds-form-element__control">
  12.                         <ui:inputText class="slds-input" aura:id="firstNameInp" value=""/>
  13.                     </div>                
  14.                 </div>
  15.             </div>     
  16.         </div>     
  17.  
  18.         <div class="slds-grid slds-wrap">
  19.             <div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">         
  20.                 <div class="slds-form-element">
  21.                     <label class="slds-form-element__label">Email</label>
  22.                     <div class="slds-form-element__controller">
  23.                         <ui:inputEmail class="slds-input" value="" aura:id="emailId"/>
  24.                     </div>
  25.                 </div>
  26.             </div>  
  27.         </div>
  28.  
  29.         <div class="slds-grid slds-wrap">     
  30.             <div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">   
  31.                 <div class="slds-form-element">
  32.                     <label class="slds-form-element__label">Phone</label>
  33.                     <div class="slds-form-element__controller">
  34.                         <ui:inputPhone class="slds-input" value="" aura:id="phoneNo"/>
  35.                     </div>
  36.                 </div>
  37.             </div>   
  38.         </div>
  39.  
  40.         <div class="slds-grid slds-wrap">
  41.             <div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium"> 
  42.                 <div class="slds-form-element">
  43.                     <label class="slds-form-element__label">Country</label>
  44.                     <div class="slds-form-element__controller">
  45.                         <ui:inputSelect class="slds-select" aura:id="countryName" value="">
  46.                             <ui:inputSelectOption text="--None--"/>
  47.                             <ui:inputSelectOption text="India"/>
  48.                             <ui:inputSelectOption text="Nepal"/>
  49.                             <ui:inputSelectOption text="Sir Lanka"/>
  50.                             <ui:inputSelectOption text="America"/>
  51.                         </ui:inputSelect>
  52.                     </div>
  53.                 </div>
  54.             </div>      
  55.         </div>        
  56.     </div>
  57. </aura:component>

 

Step 5:- Create Lightning Component : eventValidationCmpController.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

eventValidationCmpController.js [JavaScript Controller]

  1.    ({
  2. 	eventInpValidation : function(component, event, helper) {
  3.          var isValidate = true;		
  4.  
  5.         var firstNameInp = component.find('firstNameInp');
  6.         var firstNameVal= component.find('firstNameInp').get('v.value');
  7.  
  8.         var emailIdInp = component.find('emailId');
  9.         var emailIdVal= component.find('emailId').get('v.value');
  10.  
  11.         var phoneNoInp = component.find('phoneNo');
  12.         var phoneNoVal= component.find('phoneNo').get('v.value');
  13.  
  14.  
  15.         var countryName = component.find('countryName');
  16.         var countryNameVal= component.find('countryName').get('v.value');
  17.  
  18.  
  19.         if($A.util.isUndefinedOrNull(firstNameVal) || $A.util.isUndefined(firstNameVal) || $A.util.isEmpty(firstNameVal)){
  20.             firstNameInp.set("v.errors",[{message:'Name field is required'}]);
  21.             isValidate = false;
  22.         }else{
  23.             firstNameInp.set("v.errors",null);
  24.         } 
  25.  
  26.         if($A.util.isUndefinedOrNull(emailIdVal) || $A.util.isUndefined(emailIdVal) || $A.util.isEmpty(emailIdVal)){
  27.             emailIdInp.set("v.errors",[{message:'Email field is required'}]);
  28.             isValidate = false;
  29.         }else{
  30.             emailIdInp.set("v.errors",null);
  31.         }
  32.  
  33.         if($A.util.isUndefinedOrNull(phoneNoVal) || $A.util.isUndefined(phoneNoVal) || $A.util.isEmpty(phoneNoVal)){
  34.             phoneNoInp.set("v.errors",[{message:'Phone field is required'}]);
  35.             isValidate = false;
  36.         }else{
  37.             phoneNoInp.set("v.errors",null);
  38.         }
  39.  
  40.         if(countryNameVal == '--None--'){
  41.             countryName.set("v.errors",[{message:'Please select country option'}]);
  42.             isValidate = false;
  43.         }else{
  44.             countryName.set("v.errors",null);
  45.         }
  46.  
  47.         if(isValidate){
  48.             alert('Form submitted successfully');            
  49.          /* 
  50.             var toastEvent = $A.get("e.force:showToast");
  51.                 toastEvent.setParams({
  52.                     "title":"Success",
  53.                     "type":"success",
  54.                     "message":"Form submitted successfully.",                        
  55.                 });
  56.                 toastEvent.fire();
  57.              
  58.             $A.get('e.force:refreshView').fire();
  59.           */
  60.         }
  61.  
  62. 	},
  63.  
  64. })

 

Step 6:- Create Lightning Component : eventValidationCmp.css

From Developer Console >> File >> New >> Lightning Component >> Component Style CSS

eventValidationCmp.css [Style CSS]

  1.   .THIS {
  2. }
  3. .THIS .slds-input.has-error, .THIS .slds-select.has-error {
  4.     background-color: rgb(255, 255, 255);
  5.     border-color: rgb(194, 57, 52);
  6.     box-shadow: rgb(194, 57, 52) 0 0 0 1px inset;
  7.     background-clip: padding-box;
  8. }
  9.  
  10. .THIS .slds-input.has-error + ul.has-error, .THIS .slds-select.has-error + ul.has-error{border:2px #c23934 solid; border-radius: 3px;}
  11. .THIS .slds-input.has-error + ul.has-error, .THIS .slds-select.has-error + ul.has-error{position:relative; margin:0 0 10px 0; padding:0; list-style:none; display: inline-flex; top:11px; left:0;}
  12. .THIS ul.has-error li.form-element__help{margin:0; padding: 3px 5px; top:10px;}
  13. .THIS ul.has-error li.form-element__help:before{content: ''; width: 0; height: 0; border-left: 5px solid transparent; border-bottom: 10px solid #c23934;
  14.     border-right: 5px solid transparent; position: absolute; left: 0px; top:-11px; }

custom validation in lightning component -- w3web.net

 
 

Further post that would you like to learn in Salesforce

 
 

 

 

FAQ (Frequently Asked Questions)

How do you call a child method from the parent component?

One way to call a child component's function from its parent is with the help of the useRef hook. Here's the gist of it: We pass a Ref to a child component and assign the desired function to the Ref's current key. This then gives you access to that very function in the parent component.

How do you open lightning component from another lightning component?

To navigate from a Lightning component to another, specify the component name using componentDef . This example navigates to a component c:myComponent and sets a value on the contactId attribute.

How do you call one LWC from another LWC?

However, there is a workaround, if you want to navigate the user from one LWC to another LWC, you can embed LWC in an Aura Component and navigate the user from LWC to an Aura Component. Out of the box, LWC framework provides us helper methods to send the user from Lightning Web Component to an Aura Component.

Related Topics | You May Also Like

 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration




Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment