Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WTforms: Error "field not present"

Hi I have a form class which looks like below:-

class UserCreateForm(wtf.Form):
    name=wtf.TextField('Name',validators=[validators.Required(),username_check])
    email=wtf.TextField('Email')
    userimage=wtf.FileField(u'Upload Image',validators=[checkfile])

The custom validator function " checkfile" looks like this:-

def checkfile(form,field):
   if field.data:
      filename=field.data.lower()
      ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
      if not ('.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS):
            raise ValidationError('Wrong Filetype, you can upload only png,jpg,jpeg,gif files')
  else:
      raise ValidationError('field not Present') # I added this justfor some debugging.

However I find that even though I browse a file in the template and click submit , it always raises the error "field not present". I am a little confused here . Is field.data not the right way to check for the presence of filenames

like image 771
Rasmus Avatar asked Oct 30 '10 10:10

Rasmus


1 Answers

Solved this finally , had to replace field.data in the validator with field.file and then access its attributes using field.file.filename.

like image 99
Rasmus Avatar answered Oct 17 '22 21:10

Rasmus