I had to use this today. However, this does not work correctly when the set 
is empty. We assume that if the set is empty, whatever variable value being 
tested is not in the set.

Here's an updated __call__ function:

    def __call__(self, value): 
        value, error = IS_IN_SET.__call__(self, value) 
        # if IS_IN_SET says value was in the set AND the set isn't empty
        # then return an error
        if error == None and len(self.theset) > 0: 
            return (value, self.error_message) 
        else: 
            return (value, None)  



On Monday, August 23, 2010 9:19:48 AM UTC-5, Vidul Petrov wrote:
>
> Hi all, 
>
> I needed IS_NOT_IN_SET validator and added it in my custom 
> "validators.py", do you think it is useful? 
>
>
> class IS_NOT_IN_SET(IS_IN_SET): 
>     """ 
>     example:: 
>
>         INPUT(_type='text', _name='name', 
>               requires=IS_NOT_IN_SET(['max', 'john'],zero='')) 
>
>     the argument of IS_NOT_IN_SET must be a list or set 
>
>         >>> IS_NOT_IN_SET(['max', 'john'])('max') 
>         ('max', 'value not allowed') 
>         >>> IS_NOT_IN_SET(['max', 'john'])('na') 
>         ('na', None) 
>         >>> IS_NOT_IN_SET(('id1','id2'), ['first label','second 
> label'])('id100') 
>         ('id100', None) 
>         >>> IS_NOT_IN_SET(('id1','id2'), ['first label','second 
> label'])('id1') 
>         ('id1', 'value not allowed') 
>         >>> IS_NOT_IN_SET(('id1','id2'), ['first label','second 
> label'])('id2') 
>         ('id2', 'value not allowed') 
>         >>> IS_NOT_IN_SET({'id1':'first label', 'id2':'second label'}) 
> ('id100') 
>         ('id100', None) 
>         >>> IS_NOT_IN_SET({'id1':'first label', 'id2':'second label'}) 
> ('id1') 
>         ('id1', 'value not allowed') 
>         >>> IS_NOT_IN_SET({'id1':'first label', 'id2':'second label'}) 
> ('id2') 
>         ('id2', 'value not allowed') 
>         >>> import itertools 
>         >>> IS_NOT_IN_SET(itertools.chain(['1','3','5'],['2','4','6'])) 
> ('100') 
>         ('100', None) 
>         >>> IS_NOT_IN_SET(itertools.chain(['1','3','5'],['2','4','6'])) 
> ('1') 
>         ('1', 'value not allowed') 
>         >>> IS_NOT_IN_SET(itertools.chain(['1','3','5'],['2','4','6'])) 
> ('6') 
>         ('6', 'value not allowed') 
>         >>> IS_NOT_IN_SET(itertools.chain(['1','3','5'],['2','4','6'])) 
> ('7') 
>         ('7', None) 
>     """ 
>
>     def __init__(self, *a, **b): 
>         IS_IN_SET.__init__(self, *a, **b) 
>
>     def __call__(self, value): 
>         value, error = IS_IN_SET.__call__(self, value) 
>         if error == None: 
>             return (value, self.error_message) 
>         else: 
>             return (value, None) 
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to