I upgraded Flask-WTF last night from 0.13.1 -> 0.14
The issue I have is that when the FileField is blank (if the user chooses not to upload an image) then the validation fails. I do not require the field.
class PostForm(FlaskForm):
"""Handle the input from the web for posts and replies."""
body = TextAreaField('Post')
upload = FileField('Upload', [
FileAllowed(['gif', 'jpg', 'jpeg', 'png'],
'Only "gif", "jpg", "jpeg" and "png" files are supported')
])
permission = RadioField('Permission', choices=[
('0', 'Public'),
('1', 'Pjuu'),
('2', 'Approved')
], default=0)
def validate_body(self, field):
if len(field.data.strip()) == 0 and not self.upload.data:
raise ValidationError('Sorry. A message or an image is required.')
if len(field.data.replace('\r\n', '\n')) > MAX_POST_LENGTH:
raise ValidationError('Oh no! Posts can not be larger than '
'{} characters'.format(MAX_POST_LENGTH))
If I do this through my test suite all is okay using the following code:
resp = self.client.post(
url_for('posts.post'),
data={
'body': 'Test',
'upload': '',
},
follow_redirects=True
)
The the post is successful. However if I make the request through Firefox (or Chrome) the validation is triggered saying I have an invalid format.
Request body:
Content-Type: multipart/form-data; boundary=---------------------------138689464934064453715802001
Content-Length: 651
-----------------------------138689464934064453715802001
Content-Disposition: form-data; name="body"
Test
-----------------------------138689464934064453715802001
Content-Disposition: form-data; name="csrf_token"
IjY4YTZmYjNmNWM4MWJlNmVjMjc3Y2Y4YjBiMTM1Nzk3YTdhMGZkNjci.C1Vusg.RCp4SBZKai60nRqMGYP63i8JfXM
-----------------------------138689464934064453715802001
Content-Disposition: form-data; name="permission"
0
-----------------------------138689464934064453715802001
Content-Disposition: form-data; name="upload"; filename=""
Content-Type: application/octet-stream
-----------------------------138689464934064453715802001--
This was happening before I fixed the deprecation warnings that FileField
is getting removed and to use the built-in WTForms FileField
and after I changed the code to use this.
I believe it's something to do with this change but can't find any documentation.
I have had to revert the change as it stopped my site being usable.
Thanks in advance
bug