Validator: one of several fields must be non-empty

130 views
Skip to first unread message

Jim Karsten

unread,
Jun 9, 2011, 5:13:43 PM6/9/11
to web...@googlegroups.com
This validator handles a case where at least one of several fields must be non-blank.

class IS_NOT_ALL_EMPTY(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)
# Return okay either the 'value', or one of self.others is not empty.
values = []
values.append(value)
values.extend(self.others)
empties = []
for v in values:
unused_v, empty = is_empty(v)
empties.append(empty)
# Example empties == [True, True, False]
# If one False exists, it's valid
if reduce(lambda x, y: x and y, empties):
return error
return okay

Usage:

db.define_table('contact',
Field('name', 'string'
requires=IS_NOT_ALL_EMPTY([request.vars.organization],
error_message='Enter a name or an organization'),
),
Field('organization', 'string',
requires=IS_NOT_ALL_EMPTY([request.vars.name],
error_message='Enter a name or an organization'),
))

Cheers,
Jim Karsten

Anthony

unread,
Jun 9, 2011, 5:42:19 PM6/9/11
to web...@googlegroups.com
Cool. Maybe add a slice for this: http://www.web2pyslices.com/main/default/index

pbreit

unread,
Jun 9, 2011, 7:29:45 PM6/9/11
to web...@googlegroups.com
Is there an ETA on Slices2?

Bruno Rocha

unread,
Jun 9, 2011, 10:56:47 PM6/9/11
to web...@googlegroups.com
On Thu, Jun 9, 2011 at 8:29 PM, pbreit <pbreit...@gmail.com> wrote:
Is there an ETA on Slices2?

I've been working on designs and Martin Mulone in the mais app, but we both got busy woth other works, so it is frozen for a while.

Now the goal is to create a new web2pyslices which acts as a repository for apps, snippets and plugins, just like in rails where every project has a manifest file with a dependency list, than user can go to the command line and call the automatic installer (downloader-unpacker).

Thats on the way, but we do not have an ETA yet.

David Marko

unread,
Jun 10, 2011, 3:42:06 AM6/10/11
to web...@googlegroups.com
This(or some modofication) could be part of web2py core, I think its quite general purpose validator ...

David

Richard Vézina

unread,
Jun 21, 2011, 4:35:00 PM6/21/11
to web...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages