How to Display Error Messages in Lightning Web Component?

 

How to Display Error Messages in Lightning Web Component

Sample Code :

SampleForm.html

=====================

<template>

    <lightning-input label="FirstName"></lightning-input>

    <lightning-input label="LastName"></lightning-input>

    <lightning-button label="Submit"  variant="brand" onclick={handleErrors}></lightning-button>

</template>


SampleForm.js

===================

import { LightningElement,track } from 'lwc';

export default class SampleForm extends LightningElement {

    @track errors={

        "FirstName":"Please Enter the FirstName",

        "LastName":"Please Enter the LastName"

    };

    handleErrors(){

        this.template.querySelectorAll("lightning-input").forEach(item => {

            let val=item.value;

            let label=item.label;

            if(!val){

                item.setCustomValidity(this.errors[label]);

            }else{

                item.setCustomValidity("");

            }

            item.reportValidity();

        });

    }

}


Follow Us

Posted By : Sudeer Kamat Date :

view_module Related

  • How To Create Happy Christmas Lightning Card UI Design Using LWC?How To Create Happy Christmas Lightning Card UI Design Using LWC?
  • How To Preview and Download Files Using LWC?How To Preview and Download Files Using LWC?
  • How To Create News App Using LWC And Apex REST API?How To Create News App Using LWC And Apex REST API?
  • How To Create Product Lightning Card UI Design Using LWC?How To Create Product Lightning Card UI Design Using LWC?
  • How to Create Netflix Card Animation Effect In LWC?How to Create Netflix Card Animation Effect In LWC?

Comments 0