marmelab / react-admin

A frontend Framework for single-page applications on top of REST/GraphQL APIs, using TypeScript, React and Material Design
http://marmelab.com/react-admin
MIT License
24.89k stars 5.23k forks source link

No error message when trying to upload a file that exceeds a `FileInput`'s `maxSize` attribute #6441

Closed andrico1234 closed 11 months ago

andrico1234 commented 3 years ago

What you were expecting: If I try to upload a file via RA's FileInput and the file size exceeds the limit specified on the maxSize property. I expect the FileInput to not add the file to the list of files to be uploaded, and to display an appropriate error message.

What happened instead: FileInput didn't add the file to the list of files to be uploaded (good) FileInput didn't display any sort of error/validation message (bad) I'm also not getting any helpful data via the useField's meta property.

Steps to reproduce: Add this code to your Create/Edit page

const PostCreate = props => {
    return (
        <Create {...props}>
            <SimpleForm>
                <FileInput
                    source="pdffile"
                    label="PDF-Template"
                    maxSize={5000000}
                >
                    <FileField source="src" title="title" />
                </FileInput>
            </SimpleForm>
        </Create>
    );
};

Then try to upload any file.

I have a repro case here

Here's a video of the problem in action:

https://user-images.githubusercontent.com/17087167/125942480-92392ffd-69bd-4ab5-a9ed-472ad9e6b4a5.mov

The first file I chose was under 5mb, the second was over 5mb.

Environment

andrico1234 commented 3 years ago

After doing some digging, it seems that we can pass through onDropRejected as an option to FileInput. It won't solve an out-of-the box validation message, but will allow us to update helperText or display a notification.

I also don't think it gives us a reason for why the file was rejected. I'm happy to hear what you folks have to think

djhi commented 3 years ago

Thanks for reporting

djhi commented 2 years ago

To fix this, we'll have to first upgrade to the latest react-dropzone version as they added the reason why a file has been reject in v11.

However, what you're suggesting is that we treat the maxSize option (and probably all the others that filter the file such as minSize or accept) as validators.

I'm marking this as a feature request but we won't address it before v4.

magicxor commented 1 year ago

Workaround:

  const handleDropRejected = useCallback((fileRejections: FileRejection[]): void => {
    const errorMessage = fileRejections.flatMap(fr => fr.errors).map(err => err.message).join('; ');
    notify(`Invalid file(s): ${errorMessage}`, { type: 'error' });
  }, [notify]);
        <ImageInput options={{ onDropRejected: handleDropRejected }}>
          <ImageField />
        </ImageInput>
jooleanth commented 1 year ago

@magicxor Thanks! Works like a charm.

fzaninotto commented 11 months ago

There was not enough traction from the community to justify that the core team works on this feature request. So I'll close this issue, but feel free to open a PR (agains react-admin v4) that implements it.