FileRequiredValidator() doesn't work when using MultipleFileField() in my form
This works for me (found on GitHub "between the lines"):
multi_file = MultipleFileField("Upload File(s)", validators=[DataRequired()])
However
FileAllowed(["xml", "jpg"])
is ignored and does not work for me.
EDIT:No, sadly, it does not work... It returns True for form.validate()
and for form.validate_on_submit()
but when you pass no files, by deleting
required=""
from
<input id="multi_file" multiple="" name="multi_file" required="" type="file">
and submit a form, it still evaluate that as True.
So problem sill persist in full, as described...
Regarding FileAllowed validator , it is not working because FileAllowed validator expect one FileStorage object , but MultipleFileField is sending a list of Filestorage objects , which is why it is not working . You have to implement a MultiFileAllowed validator yourself. For More details StackOverflow answer explained with example.
This approach seems to solve the one part of the problem and it uses j-query, which I was unfamiliar with until this point.So since I am a novice when it comes to web-dev, I was looking for a python based approach to this one. I don't think it exists just yet.Will update when I fix the multiple file upload issue.Jsfiddle link
jQuery.validator.setDefaults({ debug: true, success: "valid"});$( "#myform" ).validate({ rules: { ":file": { required: true, accept: "image/*" } }});
and
<form id="myform"><label for="field">Required, image files only: </label><input type="file" class="left" id="field" name="field" multiple><br/><input type="submit" value="Validate!"></form><script src="https://code.jquery.com/jquery-1.11.1.min.js"></script><script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script><script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
Since I was using wtforms, I tried something called a validation class and inline validators that seems to do the trick.
"""Validation Class for RoomUpload"""class MyRoomValidator(object): def __init__(self, message=None): if not message: message = u"You need to upload the images with the category as well" self.message = message def __call__(self, form, field): print(len(form.roomImage.data)) print((form.roomTypeSelect.data)) if (not form.roomImage.data and form.roomTypeSelect.data == -1) or(form.roomImage.data and form.roomTypeSelect.data == -1) or (not form.roomImage.data and form.roomTypeSelect.data != -1):#check for all possible combinations raise ValidationError(self.message)class RoomUpload(FlaskForm): """ This is meant for the sub-form for room details upload containing the details for room and its subtype roomImage:Upload the room image roomTypeSelect:The subcategory for room category The Form will only be submitted if the images are uploaded and the RoomType will be selected """ def validate_roomTypeSelect(form, field):#Inline Validator to ensure if default choice is not chosen print(field.data) if field.data == -1: raise ValidationError('Need to Select Room Type') def validate_roomImage(form,field): for roomFile in field.data: print(roomFile.filename) if isanImageFile(roomFile.filename) == False: raise ValidationError("Error!!! Not an Image File ") roomImage = MultipleFileField(u"Room",validators=[MyRoomValidator(),FileAllowed],id = "roomImage") roomTypeSelect= SelectField("Room Type",choices=roomTypes,coerce=int,id="roomTypeSelect") roomSubmit = SubmitField("Upload the images of the room")