How to handle error reporting and validation of uploaded files using django-formset?
Short version
I'm using django-formset to create a contact form that includes a file upload field. When an incorrect file is selected, no error message is shown. When uploading the form with an incorrect file, the field is cleared and a "Please either submit a file or check the clear checkbox, not both" error is thrown for this field, which is inaccurate. Because the field is cleared I cannot validate it anymore and return a descriptive error message.
Is there any documentation that describes how this should be handled?
Long version
I want to custom validate file uploads while using django-formset. Initially, I'm mainly looking to validate image files, but later other types as well.
Problem one
Error messages do not show when selecting invalid files. Upon selecting a file that is too large, nothing happens (no errors are shown, no POST request is sent, GUI is not updated). Here I would at least expect a message to display "File size is too big" or so. When selecting a file with an invalid extension, a POST request is sent and the GUI is updated to show the selected file (with or without a valid thumbnail). However, no error message is shown. I expected a message to be shown such as "This file type is not allowed".
As I understand, JSON responses are not processed when performing the file selection. Thus creating specific JSON responses will not work out of the box. Is there another way to show these error messages? For example by making use of the <meta name="error-messages" type_mismatch="File upload still in progress." pattern_mismatch="This is not a valid file." bad_input="File upload failed.">
element?
Problem two
Somewhat related: when submitting the form with an invalid file (e.g. wrong extension), I always receive the form error {"uploaded_file": [{"message": "Please either submit a file or check the clear checkbox, not both.", "code": "contradiction"}]}
. Which seems to be because the input field is made empty/removed from form.cleaned_data if the file is invalid. I would like to display a more useful message, such as that the extension is incorrect. However, I cannot validate the file anymore if it is missing from my data.
The main thing I'm looking for is some documentation. How are these things supposed to be handled? I'm feeling like I just overlooked something perhaps? Any help is greatly appreciated.
I'm using Django 5.2.4, Django CMS 4.1.4, and django-formset 2.0.1
My form class is very straightforward:
class ContactForm(forms.Form):
name = fields.CharField(widget=widgets.TextInput(attrs={"placeholder": ""}))
email_address = fields.EmailField(widget=widgets.TextInput(attrs={"placeholder": ""}))
subject = fields.CharField(widget=widgets.TextInput(attrs={"placeholder": ""}))
message = fields.CharField(widget=widgets.Textarea(attrs={"placeholder": "","rows":"5"}))
uploaded_file = FileField(
label="Image (optional)",
widget=UploadedFileInput(attrs={
'accept': 'image/jpeg,image/png,image/gif',
'max-size': MAX_UPLOAD_SIZE,
}),
help_text="JPEG, PNG, or GIF. Max file size 5 MB. We accept other formats after initial contact.",
required=False,
)
Note that I've also posted this question on github: https://github.com/jrief/django-formset/discussions/226