I found that all(empties) does the same of reduce(lambda x, y: x and y,
empties)...
And I write this for the case when we want only one field to be filled for a
group of fields passed to the validator :
class ONLY_ONE_CAN_BE_FILLED(object):
"""Class representing a validator requiring at least one non-empty field
in a set. """
def __init__(
self,
others,
error_message='Enter a value in at least one field'
):
self.others = others
self.error_message = error_message
def __call__(self, value):
okay = (value, None)
error = (value, self.error_message)
values = []
values.append(value)
values.extend(self.others)
empties = []
for v in values:
unused_v, empty = is_empty(v)
empties.append(empty)
if empties.count(False) == 1:
return okay
else:
return error
Here how to use it :
requires=ONLY_ONE_CAN_BE_FILLED([request.vars.FIELD1,request.vars.FIELD2],error_message='Select
only one field')
It's a bit counter intuitive to count the False, but since we only have
is_empty()...
Richard
On Fri, Jun 10, 2011 at 3:42 AM, David Marko <[email protected]> wrote:
> This(or some modofication) could be part of web2py core, I think its quite
> general purpose validator ...
>
> David
>