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
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]
<aura:application extends="force:slds">
<c:eventCmp/>
</aura:application>
Step 2:- Create Lightning Component : eventCmp.cmp
From Developer Console >> File >> New >> Lightning Component
eventCmp.cmp [Lightning Component File]
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
<c:eventValidationCmp aura:id="getEventId"/>
<div class="slds-grid slds-wrap">
<div class="slds-text-align--center slds-col slds-size_6-of-12 slds-m-bottom--medium">
<button class="slds-button slds-button--brand" onclick="{!c.eventValidation}">Submit</button>
</div>
<br/><br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<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>
<br/><br/>
<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>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<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>
<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>
<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>
<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>
<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>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<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>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</div>
</aura:component>
Step 3:- Create Lightning Component : eventCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
eventCmpController.j [JavaScript Controller]
({
eventValidation : function(component, event, helper) {
var firstNameId1 = component.find('firstNameId');
component.find('getEventId').validationDR(firstNameId1);
},
})
Step 4:- Create Lightning Component : eventValidationCmp.cmp
From Developer Console >> File >> New >> Lightning Component
eventValidationCmp.cmp [Lightning Component File]
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
<aura:attribute name="fieldVal" type="string"/>
<aura:method name="validationDR" action="{!c.eventInpValidation}">
</aura:method>
<div class="slds slds-p-horizontal--medium">
<div class="slds-grid slds-wrap">
<div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
<div class="slds-form-element">
<label class="slds-form-element__label">Name</label>
<div class="slds-form-element__control">
<ui:inputText class="slds-input" aura:id="firstNameInp" value=""/>
</div>
</div>
</div>
</div>
<div class="slds-grid slds-wrap">
<div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
<div class="slds-form-element">
<label class="slds-form-element__label">Email</label>
<div class="slds-form-element__controller">
<ui:inputEmail class="slds-input" value="" aura:id="emailId"/>
</div>
</div>
</div>
</div>
<div class="slds-grid slds-wrap">
<div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
<div class="slds-form-element">
<label class="slds-form-element__label">Phone</label>
<div class="slds-form-element__controller">
<ui:inputPhone class="slds-input" value="" aura:id="phoneNo"/>
</div>
</div>
</div>
</div>
<div class="slds-grid slds-wrap">
<div class="slds-p-horizontal--medium slds-col slds-size_6-of-12 slds-m-bottom--medium">
<div class="slds-form-element">
<label class="slds-form-element__label">Country</label>
<div class="slds-form-element__controller">
<ui:inputSelect class="slds-select" aura:id="countryName" value="">
<ui:inputSelectOption text="--None--"/>
<ui:inputSelectOption text="India"/>
<ui:inputSelectOption text="Nepal"/>
<ui:inputSelectOption text="Sir Lanka"/>
<ui:inputSelectOption text="America"/>
</ui:inputSelect>
</div>
</div>
</div>
</div>
</div>
</aura:component>
Step 5:- Create Lightning Component : eventValidationCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
eventValidationCmpController.js [JavaScript Controller]
({
eventInpValidation : function(component, event, helper) {
var isValidate = true;
var firstNameInp = component.find('firstNameInp');
var firstNameVal= component.find('firstNameInp').get('v.value');
var emailIdInp = component.find('emailId');
var emailIdVal= component.find('emailId').get('v.value');
var phoneNoInp = component.find('phoneNo');
var phoneNoVal= component.find('phoneNo').get('v.value');
var countryName = component.find('countryName');
var countryNameVal= component.find('countryName').get('v.value');
if($A.util.isUndefinedOrNull(firstNameVal) || $A.util.isUndefined(firstNameVal) || $A.util.isEmpty(firstNameVal)){
firstNameInp.set("v.errors",[{message:'Name field is required'}]);
isValidate = false;
}else{
firstNameInp.set("v.errors",null);
}
if($A.util.isUndefinedOrNull(emailIdVal) || $A.util.isUndefined(emailIdVal) || $A.util.isEmpty(emailIdVal)){
emailIdInp.set("v.errors",[{message:'Email field is required'}]);
isValidate = false;
}else{
emailIdInp.set("v.errors",null);
}
if($A.util.isUndefinedOrNull(phoneNoVal) || $A.util.isUndefined(phoneNoVal) || $A.util.isEmpty(phoneNoVal)){
phoneNoInp.set("v.errors",[{message:'Phone field is required'}]);
isValidate = false;
}else{
phoneNoInp.set("v.errors",null);
}
if(countryNameVal == '--None--'){
countryName.set("v.errors",[{message:'Please select country option'}]);
isValidate = false;
}else{
countryName.set("v.errors",null);
}
if(isValidate){
alert('Form submitted successfully');
/*
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
"title":"Success",
"type":"success",
"message":"Form submitted successfully.",
});
toastEvent.fire();
$A.get('e.force:refreshView').fire();
*/
}
},
})
Step 6:- Create Lightning Component : eventValidationCmp.css
From Developer Console >> File >> New >> Lightning Component >> Component Style CSS
eventValidationCmp.css [Style CSS]
.THIS {
}
.THIS .slds-input.has-error, .THIS .slds-select.has-error {
background-color: rgb(255, 255, 255);
border-color: rgb(194, 57, 52);
box-shadow: rgb(194, 57, 52) 0 0 0 1px inset;
background-clip: padding-box;
}
.THIS .slds-input.has-error + ul.has-error, .THIS .slds-select.has-error + ul.has-error{border:2px #c23934 solid; border-radius: 3px;}
.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;}
.THIS ul.has-error li.form-element__help{margin:0; padding: 3px 5px; top:10px;}
.THIS ul.has-error li.form-element__help:before{content: ''; width: 0; height: 0; border-left: 5px solid transparent; border-bottom: 10px solid #c23934;
border-right: 5px solid transparent; position: absolute; left: 0px; top:-11px; }
Further post that would you like to learn in Salesforce
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 |