Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FileRequired more consistent with InputRequired #108

Merged
merged 3 commits into from
Jan 7, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions flask_wtf/file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from werkzeug import FileStorage
from wtforms import FileField as _FileField
from wtforms import ValidationError
from wtforms.validators import InputRequired, StopValidation


class FileField(_FileField):
Expand All @@ -21,7 +22,7 @@ def has_file(self):
return self.data.filename not in [None, '', '<fdopen>']


class FileRequired(object):
class FileRequired(InputRequired):
"""
Validates that field has a file.

Expand All @@ -30,12 +31,13 @@ class FileRequired(object):
You can also use the synonym **file_required**.
"""

def __init__(self, message=None):
self.message = message

def __call__(self, form, field):
if not field.has_file():
raise ValidationError(self.message)
if self.message is None:
message = field.gettext('This field is required.')
else:
message = self.message
raise StopValidation(message)

file_required = FileRequired

Expand Down