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

Add a BooleanRequired validator for use with BooleanField #481

Closed
wants to merge 3 commits into from
Closed
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
22 changes: 22 additions & 0 deletions src/wtforms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,27 @@ def __call__(self, hostname):
return True


class BooleanRequired(object):
"""
Validates that boolean input was provided for this field.
"""

field_flags = ("required",)

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

def __call__(self, form, field):
if not field.raw_data or field.raw_data[0] not in (True, False):
if self.message is None:
message = field.gettext("This field is required.")
else:
message = self.message

field.errors[:] = []
raise StopValidation(message)


email = Email
equal_to = EqualTo
ip_address = IPAddress
Expand All @@ -662,3 +683,4 @@ def __call__(self, hostname):
url = URL
any_of = AnyOf
none_of = NoneOf
boolean_required = BooleanRequired