How to add dynamically form validation with required field error message in lightning-input field in Lightning Web Component – LWC | Create dynamically form validation for required field in lwc

6,628 views


Hey guys, today in this post we are going to learn about How to add dynamically form validation with required field error message in lightning-input field in Lightning Web Component – LWC.

Files we used in this post example

lwcDynamicFormValidation.html LWc HTML File Template HTML file for used to create HTML lightning-input-field form validation
lwcDynamicFormValidation.js LWC JavaScript File It is holding Form Validation action function that fire on click button and diaplay required error message.
lwcDynamicFormValidation.js-meta.xml XML Meta File It is used for where this lightning web component File you want to expose.
lwcDynamicFormValidationApp.app Lightning Application It is used for call the LWC HTML file to preview on browser.

Live Demo

retrieve data from multiple custom objects in visualforce -- w3web.net

 

You can download file directly from github by Click Here.

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

 
 

Other related post that would you like to learn in Salesforce

Create Lightning Web Component HTML

Step 1:- Create Lightning Web Component HTML ➡ lwcDynamicFormValidation.html

SFDX:Lightning Web Component ➡ New ➡ lwcDynamicFormValidation.html

lwcDynamicFormValidation.html [Lightning Web Component HTML]

  1.   <template>
  2.     <lightning-card title="Create a dynamically form validation in Lightning web component - LWC" icon-name="custom:custom15" size="small"> 
  3.         <div class="slds-grid slds-wrap">
  4.             <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--medium">
  5.                 <lightning-input type="text" label="First Name"></lightning-input>
  6.              </div>
  7.  
  8.              <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--medium">
  9.                 <lightning-input type="text" label="Last Name" ></lightning-input>
  10.              </div>
  11.  
  12.              <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--medium">
  13.                 <lightning-Input type="text" label="Email"></lightning-input>
  14.              </div>
  15.  
  16.              <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--medium">
  17.                 <lightning-Input type="phone" label="Phone"></lightning-input>
  18.              </div>
  19.  
  20.              <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--medium">
  21.                <lightning-input type="text" label="State" ></lightning-input>
  22.              </div>
  23.  
  24.              <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--medium">
  25.                <lightning-input type="text" label="City" ></lightning-input>
  26.              </div>
  27.  
  28.              <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--medium">
  29.                <lightning-input type="number" label="Pincode" ></lightning-input>
  30.              </div>
  31.  
  32.              <div class="slds-col slds-size_6-of-12 slds-p-horizontal--medium slds-m-bottom--medium">
  33.                <lightning-input type="text" label="Address" ></lightning-input>
  34.              </div>
  35.  
  36.  
  37.             <div class="slds-col slds-size_12-of-12 slds-text-align_center slds-m-top--small">
  38.                  <lightning-button label="Validate Form Action" variant="brand" onclick={validateAction} title="Validation Action"></lightning-button>               
  39.             </div>
  40.          </div>
  41.  
  42. <br/><br/>
  43.    <!--Start RelatedTopics Section-->
  44. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  45.  
  46.             <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>
  47.  
  48.             <br/><br/>
  49.             <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>
  50.             <div style="display:block; overflow:hidden;"> 
  51.                 <div style="width: 50%; float:left; display:inline-block">
  52.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  53.                         <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>
  54.                         <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>
  55.                         <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>
  56.                         <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>
  57.                         <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>
  58.                     </ul>
  59.             </div>
  60.  
  61.             <div style="width: 50%; float:left; display:inline-block">
  62.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  63.                         <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>
  64.                         <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>
  65.                         <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>
  66.                         <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>
  67.                         <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>
  68.                     </ul>
  69.                 </div>
  70.                <div style="clear:both;"></div> 
  71.                <br/>
  72.                 <div class="youtubeIcon">
  73.                     <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>
  74.                 </div>
  75.     </div>
  76.  
  77. </div>
  78.  
  79.   <!--End RelatedTopics Section-->
  80.     </lightning-card>
  81. </template>

Create Lightning Web Component Javascript

Step 2:- Create Lightning Web Component Javascript ➡ lwcDynamicFormValidation.js

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

SFDX:Lightning Web Component ➡ New ➡ lwcDynamicFormValidation.js

lwcDynamicFormValidation.js [LWC JavaScript File]

  1.    import { LightningElement } from 'lwc';
  2.  
  3. export default class LwcDynamicFormValidation extends LightningElement {
  4.     validateAction(event){
  5.         let fieldErrorMsg="Please Enter the";
  6.         this.template.querySelectorAll("lightning-input").forEach(item => {
  7.             let fieldValue=item.value;
  8.             let fieldLabel=item.label;            
  9.             if(!fieldValue){
  10.                 item.setCustomValidity(fieldErrorMsg+' '+fieldLabel);
  11.             }
  12. 			else{
  13.                 item.setCustomValidity("");
  14.             }
  15.             item.reportValidity();
  16. 		});
  17.     }
  18. }

Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component Meta XML ➡ lwcDynamicFormValidation.js-meta.xml

SFDX:Lightning Web Component ➡ New >> lwcDynamicFormValidation.js-meta.xml

lwcDynamicFormValidation.js-meta.xml [LWC Meta Data XML]

  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.       </targets>
  10. </LightningComponentBundle>

Create Lightning Application

Step 4:- Create Lightning Application ➡ lwcDynamicFormValidationApp.app

From Developer Console ➡ File ➡ New ➡ Lightning Application

lwcDynamicFormValidationApp.app [Lightning Component File]

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

dynamically form validation required field in lwc -- www.w3web.net

Further post that would you like to learn in Salesforce

 


 

FAQ (Frequently Asked Questions)

What is form validation?

Form validation is a “technical process where a web-form checks if the information provided by a user is correct.” The form will either alert the user that they messed up and need to fix something to proceed.

What are the validation rules in Salesforce?

Validation rules in salesforce contains a formula or expressions that evaluates the data in one or more fields in a record to meet the standards and returns a value “True” or “False”.

What is system validation and custom validation in Salesforce?

System validations are the validation which are enforced by Salesforce like number field should not contains alphabets, phone number should be in valid format etc.. While custom validation rule are the one which are created manually under object.

Related Topics | You May Also Like

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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