-1

I want to check if field is empty before moving to other part of form divided by div. on a button(not submit) have tried custom validation as given in documentation lightning-input-documentation but i'm getting error

[Cannot read property 'setCustomValidity' of undefined]

HTML:

<lightning-input-field
    required
    field-name="AccountId"
    value={account__c}
    class="inputCmp"
></lightning-input-field>

JS:

var inputCmp = this.template.querySelector(".inputCmp");
var value = inputCmp.value;
 
if (!value) {
    inputCmp.setCustomValidity("Please select account");
} else {
    inputCmp.setCustomValidity(""); 
}
inputCmp.reportValidity(); 
4
  • Where have you written this piece of js code? Possible to share a minimal version of code minus the dependencies, that we can use to replicate the behavior?
    – Raul
    Commented Apr 18, 2021 at 13:23
  • Did you read the error?
    – Andy Ray
    Commented Apr 18, 2021 at 21:29
  • I have written this code on button click action and the component is in action button. Commented Apr 19, 2021 at 12:30
  • yes I have read error, im able to get value from that field but cant set customvalidity as shown in documentation Commented Apr 19, 2021 at 12:32

2 Answers 2

0

Are you executing that in the connectedCallback()? If so, move it to renderedCallback(). In the connectedCallback the dom is not still available.

Edit: sorry but I didn't noticed you were executing it from a button. The code confused me and I wasn't allowed to write comments yet.

As I wrote below, I suppose that the problem is that the class is being set in the input shadow DOM, so you don't have access to it.

You can try with querySelector("lightning-input-field") if it's the only input, or query it in another way.

Edit2: You can also do it like this:

const inputFields = this.template.querySelectorAll(
    'lightning-input-field'
);
if (inputFields) {
    inputFields.forEach(field => {
        if(field.name === "AccountId) {
            //Your Logic
        }
    });
}
6
  • I am executing it on a button event. So, I don't think i need connectedCallback or renderedCallback and I am using this component in action button. Commented Apr 19, 2021 at 3:20
  • That's not shown in your code :) although you specified it, my fault. I suppose then, that the class maybe is being set in the input shadow DOM, so you don't have access to it.You can try with querySelector("lightning-input-field") if it's the only input, or query it in another way. Commented Apr 19, 2021 at 6:24
  • Please read carefully I have mentioned it in explanation. Commented Apr 19, 2021 at 6:26
  • I have more than one input field, some them are mandatory I want to check only for mandatory field. querySelector("lightning-input-field") will consider all input fields. Commented Apr 19, 2021 at 13:20
  • You can wrap it into another element and query by "parentClass > lightning-input-field" or by nth child. If you share the html I can tell you extactly. Commented Apr 19, 2021 at 17:56
0

For field "AccountId" you can use this object hierarchy to set error to lightning-input-field.

handleSubmit(events){
  let f = this.template.querySelector('.inputCmp');
  f.setErrors({'body':{'output':{'fieldErrors':{'AccountId':[{'message':'Error'}]}}}});
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .