You need to sign in to do that
Don't have an account?
kavita 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
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."
);
}
});
}
}
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."
);
}
});
}
}
All Answers
Try callling the setCustomValidity method on inputfield variable instead of the inputValue
I tried both Inputfield and inputvalue but the same error persists
Yes I found that out later setCustomValidity works only for lightning-input but not for lightning-input-field.