Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Created April 9, 2014 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThiefMaster/10243751 to your computer and use it in GitHub Desktop.
Save ThiefMaster/10243751 to your computer and use it in GitHub Desktop.
class UsedIf(object):
"""
Make a WTF field "used" if a given condition evaluates to True.
If the field is not used, validation stops.
"""
def __init__(self, condition):
self.condition = condition
def __call__(self, form, field):
if self.condition in (True, False):
if not self.condition:
field.errors[:] = []
raise StopValidation()
elif not self.condition(form, field):
field.errors[:] = []
raise StopValidation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment