flask-wtf
flask-wtf copied to clipboard
Issue with multiple file validation
Hey, I'm having trouble with validating a wtforms.MultipleFileField
field.
Creating a form field like so:
not_important = wtforms.MultipleFileField("Multiple file test",[FileRequired("File required"), FileAllowed(['csv'], ".csv file required")])
doesn't really work. After passing one or more files to the respective field, the validation returns "File required" error.
But the multi-dict of request.files
shows that all files are read correctly.
Sample:
ImmutableMultiDict([('not_important', <FileStorage: 'Headline_all_test.csv' ('application/vnd.ms-excel')>)]
So basically the issue is - flask requests reads files passed, but the form.validate_on_submit()
errors out telling me, that no file has been passed.
BTW - found this fresh SO thread pertaining to the same issue: https://stackoverflow.com/questions/59734277/filerequiredvalidator-doesnt-work-when-using-multiplefilefield-in-my-form
I think there is already an issue #337 about this problem and also a PR #338 by @greyli to solve this, but some how hasn't been merged yet. 😢
I'm having the same issue, any work around?
I'm having the same issue, any work around?
I think you can create your own field and validation by copy the code from PR #338.
I will try to implement what David recommended late this month as an alternative to #338.
I've uploaded validators for the wtforms.fields.MultipleFileField
on my Github: https://github.com/damianpawlikowski/flaskwtf-boost. I may add it as PyPI tomorrow if there will be an interest.
Validators:
flaskwtf_boost.validators.FilesRequired
Allows you to set minimum and maximum count of required files, and forcing required param on the field if necessary.
flaskwtf_boost.validators.FilesAllowed
Same as flask_wtf.file.FileAllowed
- works for list of an extensions and UploadSet(note: use pip install Flask-Reuploaded
or there may be an issues).
flaskwtf_boost.validators.FilesSize
Allows you to set minimum and maximum size for an independent file and minimum, maximum total size for an all uploads.
I've used it in the project sometime ago. You may just copy flaskwtf_boost.validators.py
flaskwtf_boost.fields.py
files somewhere in your project, for example to the utils.py
and import them and it should work.
@greyli Glad to here any progress in the future. Hope you are doing well. Now I just copy your old code and create my own field and validation as suggested by @HenriqueLin .
We really need this feature in flask wtforms, Any updates as to potentially when this could be updated please?
@greyli @HenriqueLin @robscott95
Fix released in v1.2.0