from gluon.validators import is_empty from gluon.validators import Validator class IS_NOT_EMPTY_IF_OTHER(Validator): '''Allow a field to be empty unless any some other value is present. Note: "other" field may be a list of fields. If any of them is not empty, the field must be filled. ''' def __init__(self, other, error_message='must be filled because other value ' 'is present'): self.other = other self.error_message = error_message def __call__(self, value): if isinstance(self.other, (list, tuple)): others = self.other else: others = [self.other] has_other = False for other in others: other, empty = is_empty(other) if not empty: has_other = True break value, empty = is_empty(value) if empty and has_other: return (value, T(self.error_message)) else: return (value, None)
Comments (2)
0
viniciusban 11 years ago
Put this validator in a module, then import and use it.
Or put it in some models/a_my_custom_validators.py to become global throughout your application.
More on this: http://web2py.com/books/default/chapter/29/07#Custom-validators
0
r3bhatia 11 years ago
Hi...Just wanted to know isto how do we use this custom validator?
Can you please give a small example..
Thanks
Rahul