Not allowed extension not showing in the error message.
Hlamallama opened this issue · comments
Issue Summary
Not allowed extension showing as blank when using the WAGTAILDOCS_EXTENSIONS setting.
Steps to Reproduce
- Start a new project with
wagtail start myproject
- Add WAGTAILDOCS_EXTENSIONS = ["doc", "pdf"]
- Log onto Wagtail, navigate to Documents on the side menu
- Add a new document with txt extension.
- Error thrown File extension “” is not allowed. Allowed extensions are: doc, docx, pdf. Instead of File extension “txt” is not allowed. Allowed extensions are: doc, docx, pdf
Technical details
- Python version: 3.11.5
- Django version: 5.0.3
- Wagtail version: 6.0
- Browser version: Chrome 122 and Firefox 123
Working on this
Anyone can contribute to this. View our contributing guidelines, add a comment to the issue once you’re ready to start.
It looks like there's a deeper issue here, the user shouldn't be presented with a successful upload message and then shown an error when they hit Update because the upload has actually failed already.
For example, with: WAGTAILDOCS_EXTENSIONS = ["doc", "pdf"]
:
The .webp
file hasn't actually successfully uploaded, if we navigate to the list of Documents, it's not there. Hitting Update then shows the validation error:
I think this is happening because the FileExtensionValidator
adds the error to __all__
but when checking if the form is valid, we check the "file"
key. If we add the error to the "file"
key then the validation error is caught before the file is uploaded:
The below is a potential solution to get the above result but I'm not sure it's the best solution!
In the model clean method:
if allowed_extensions:
validate = FileExtensionValidator(allowed_extensions)
try:
validate(self.file)
except ValidationError as e:
raise ValidationError(
{ "file": e.messages[0] }
)
Fixed in 5cc28ac