EP-30 | Validations for Custom Lightning Web Component Form | LWC Stack ☁️⚡️

  LWC Stack is Lightning Web Component tutorial series by Salesforce MVP Kapil Batra. In this series you will find LWC tutorials from beginner to intermediate level.

So if you are working on it or planning to learn LWC then this video series is for you. Let's learn and grow together !

Please check complete code below from LWC Stack EP-30

HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<template>
  <lightning-card title="Validate custom LWC" icon-name="utility:user">
    <div class="slds-var-m-around_medium">
      <div style="padding: 10px">
        <lightning-input
          class="slds-p-around_medium"
          label="Name"
          name="accName"
          onchange={handleNameChange}
          required
        ></lightning-input>
        <lightning-input
          class="slds-p-around_medium"
          label="Phone"
          type="phone"
          name="accPhone"
          onchange={handlePhoneChange}
          required
        ></lightning-input>
        <br />
        <lightning-button
          class="slds-m-left_x-small"
          label="Save"
          variant="brand"
          onclick={save}
        ></lightning-button>
      </div>
    </div>
  </lightning-card>
</template>
JavaScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { LightningElement } from "lwc";
import { createRecord } from "lightning/uiRecordApi";
import { ShowToastEvent } from "lightning/platformShowToastEvent";

//Object and field name definations

export default class ValidateRecordEditForm extends LightningElement {
  accName;
  accPhone;

  handleNameChange(event) {
    this.accName = event.target.value;
  }
  handlePhoneChange(event) {
    this.accPhone = event.target.value;
  }

  save() {
    const isInputsCorrect = [
      ...this.template.querySelectorAll("lightning-input")
    ].reduce((validSoFar, inputField) => {
      inputField.reportValidity();
      return validSoFar && inputField.checkValidity();
    }, true);
    if (isInputsCorrect) {
      var fields = {
        Name: this.accName,
        Phone: this.accPhone
      };

      var objRecordInput = { apiName: "Account", fields };
      createRecord(objRecordInput)
        .then((response) => {
          this.dispatchEvent(
            new ShowToastEvent({
              title: "Success",
              message: "Account Created Successfully",
              variant: "success"
            })
          );
        })
        .catch((error) => {
          this.dispatchEvent(
            new ShowToastEvent({
              title: "Error",
              message: error.message,
              variant: "error"
            })
          );
        });
    }
  }
}
Checkout complete video tutorial below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks

Happy Coding :)

Post a Comment

0 Comments