I have an lwc form that consists of a lightning-record-edit-form and some lightning-input-fields
I have to override the submit button with apex as I have to perform some custom logic.
There are some validation rules defined against the lightning-input-field which I am trying to fire using reportValidity() which fires correctly. But after the rule fires, I want to stop execution. This is not happening and the code is still going to apex. How can I stop execution if reportValidity() throws up errors on lightning-input-field?
Here is the code HTML
<lightning-record-edit-form object-api-name={objName} onsubmit={handleSubmit} onsuccess={handleSuccess} record-id={recordId}>
<lightning-input-field field-name={fieldName} name = {fieldName} onchange={handleChange}></lightning-input-field>
<lightning-button variant="brand" type="submit" name="save" label="Submit" onclick={validateFields}>
</lightning-button>
</lightning-record-edit-form>
JS:
validateFields(event) {
this.template.querySelectorAll('lightning-input-field').forEach(element => {
element.reportValidity();
});
//check if there is an error. not sure how to check if reportValidity throws an error as the code proceeds to execute apex even when there is an error
let isError = this.template.querySelector(".slds-has-error");
//if no error then only call apex from handleSubmit
if(!isError) {
this.template.querySelector("lightning-record-edit-form").submit();
}
}