flask-wtf icon indicating copy to clipboard operation
flask-wtf copied to clipboard

Issue with multiple file validation

Open robscott95 opened this issue 5 years ago • 5 comments

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

robscott95 avatar Feb 02 '20 23:02 robscott95

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. 😢

HenriqueLin avatar Feb 28 '21 21:02 HenriqueLin

I'm having the same issue, any work around?

snunzi avatar Jan 11 '22 19:01 snunzi

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.

HenriqueLin avatar Jan 11 '22 19:01 HenriqueLin

I will try to implement what David recommended late this month as an alternative to #338.

greyli avatar Jan 12 '22 03:01 greyli

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.

damianpawlikowski avatar Jan 14 '22 21:01 damianpawlikowski

@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 .

iROCKBUNNY avatar Mar 08 '23 10:03 iROCKBUNNY

We really need this feature in flask wtforms, Any updates as to potentially when this could be updated please?

@greyli @HenriqueLin @robscott95

khanfarhan10 avatar Aug 15 '23 21:08 khanfarhan10

Fix released in v1.2.0

azmeuk avatar Oct 01 '23 10:10 azmeuk