function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kavita vedulakavita vedula 

setCustomValidity is not a function error in lwc

I 'm trying to validate a lightning inputfield and throw custom error. Below is the code snippet for LWC and js file. I 'm getting the below error
User-added image
html file 

<!-- Body -->
<div class="slds-p-horizontal_medium slds-m-top_medium">
<lightning-record-edit-form object-api-name="CaseContactRole">
<lightning-input-field
class="contactLookup"
field-name="ContactId"
onchange={contactLookupHandle}
>
</lightning-input-field>
</lightning-record-edit-form>
</div>

JS file

contactLookupHandle(event) {
this.selectedContactId = event.target.value;
this.validateFields();
}
validateFields() {
let inputfield = this.template.querySelector(".contactLookup");
console.log("Inputtttttttt::::::::::" + inputfield);
let inputValue = inputfield.value;
if (!inputValue) {
inputfield.reportValidity();
} else {
this.existingContactRoles.forEach((contactRole) => {
console.log("Contact Role from input :::::::::" + inputValue);
console.log(
"Contact Role already existing :::::::::" + contactRole.ContactId
);
if (inputValue === contactRole.ContactId) {
inputValue.setCustomValidity(
"This Contact has already been CC'd onto the Case."
);
}
});
}
}


 
Best Answer chosen by kavita vedula
Irina IvanovaIrina Ivanova
I am having the same issue and I think that setCustomValidity works only for  lightning-input but not for lightning-input-field.

All Answers

McCuboMcCubo
Hi @kavita,
Try callling the setCustomValidity method on inputfield variable instead of the inputValue
kavita vedulakavita vedula
Hi Miguel,

I tried both Inputfield and inputvalue but the same error  persists
 
Irina IvanovaIrina Ivanova
I am having the same issue and I think that setCustomValidity works only for  lightning-input but not for lightning-input-field.
This was selected as the best answer
kavita vedulakavita vedula
Hi Irina Ivanova,
Yes I found that out later setCustomValidity works only for  lightning-input but not for lightning-input-field.