views:

25

answers:

0
+1  Q: 

WTforms validation

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

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