Created
April 9, 2014 08:54
-
-
Save ThiefMaster/10243751 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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