1

I need to show a custom error message in lightning-file-upload when we tried to upload the unacceptable format of the file.

<lightning-file-upload
            label="Attach receipt"
            name="fileUploader"
            accept={acceptedFormats}
            record-id={myRecordId}
            onuploadfinished={handleUploadFinished}
            multiple>
</lightning-file-upload>

it does not have any property to set custom error messages?

What I tried so far?

I have tried accessing the component in the renderedCallback method. But No success.

Update:-

I cannot use toast to show error in lightning-file-upload because it does not have any such method like onerror.

  • Is it still calling the handleUploadFinished method when the file format is incorrect? If so, you should just handle the error there and delete the file if it actually saved to Salesforce. If not, I can't help you there. Still just beginning my LWC training (I'm pretty much working with just aura components at the moment). – Roti Oct 18 at 20:39
0

In the docs it says you can use your custom validity error messages using HTML 5's api.

<template>
    <lightning-input class="inputCmp"
                     label="Enter your name:">
    </lightning-input>
    <lightning-button label="Register"
                      onclick={register}>
    </lightning-button>
 </template>
import { LightningElement } from 'lwc';
export default class MyComponent extends LightningElement {

    register(event) {
        var inputCmp = this.template.querySelector(".inputCmp");
        var value = inputCmp.value;
        // is input valid text?
        if (value === "John Doe") {
            inputCmp.setCustomValidity("John Doe is already registered");
        } else {
            inputCmp.setCustomValidity(""); // if there was a custom error before, reset it
        }
        inputCmp.reportValidity(); // Tells lightning-input to show the error right away without needing interaction
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.