Skip to content

Instantly share code, notes, and snippets.

@glenrobertson
Created June 15, 2015 21:09
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 glenrobertson/cdec0335c8574e3d502e to your computer and use it in GitHub Desktop.
Save glenrobertson/cdec0335c8574e3d502e to your computer and use it in GitHub Desktop.
Conditional DataRequired wtforms validator
class DataRequiredIf(wtforms.validators.DataRequired):
"""
Similar to DataRequired
Only evaluates if func(form.data) evaluates to True
"""
def __init__(self, func, *args, **kwargs):
super(DataRequiredIf, self).__init__(*args, **kwargs)
self.func = func
def __call__(self, form, field):
if self.func(form.data):
super(DataRequiredIf, self).__call__(form, field)
else:
raise wtforms.validators.StopValidation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment