18 lines
711 B
Python
18 lines
711 B
Python
from wtforms.validators import Optional
|
|
|
|
|
|
class ConditionalValidator(object):
|
|
def __init__(self, other_field_name, other_data_contents, dependent_validator):
|
|
self.other_field_name = other_field_name
|
|
self.other_data_contents = other_data_contents
|
|
self.dependent_validator = dependent_validator
|
|
|
|
def __call__(self, form, field):
|
|
other_field = form._fields.get(self.other_field_name)
|
|
if other_field is None:
|
|
raise Exception('no field named "%s" in form' % self.other_field_name)
|
|
if other_field.data in self.other_data_contents:
|
|
self.dependent_validator.__call__(form, field)
|
|
else:
|
|
Optional().__call__(form, field)
|