IS_IN_SET_OR validator

0 views
Skip to first unread message

DenesL

unread,
Sep 3, 2008, 5:02:45 PM9/3/08
to web2py Web Framework
Any use for the following validator?
Note 1: IS_NULL_OR would be special case where theset=[""]
Note 2: it can still generate a dropdown with all the values from
self.options() plus self.other.options()
Note 3: gluon/sqlhtml.py modification also required
Note 4: more specific error message handling can be applied to
IS_NULL_OR

class IS_IN_SET_OR(object):
def __init__(self, other, theset=[], labels=None,
error_message='value not in set'):
self.other=other
self.theset=[str(i) for i in theset]
self.labels=labels
self.error_message=error_message
def set_self_id(self,id):
if hasattr(self.other,'set_self_id'):
self.other.set_self_id(id)
def __call__(self,value):
if value in self.theset:
return(value,None)
v,e=self.other(value)
if e:
return(v,'%s and %s'%(self.error_message,e))
else:
return(v,self.error_message)
def formatter(self,value):
if hasattr(self.other,'formatter'):
return self.other.formatter(value)
return value
def options(self):
if self.labels:
return [(k,self.labels[i]) for i,k in
enumerate(self.theset)]
else:
return [(k,k) for k in self.theset]

mdipierro

unread,
Sep 3, 2008, 11:45:59 PM9/3/08
to web2py Web Framework
Thank you Denes. We need to build a repository of these. for now, ould
you write an AlterEgo page?

Massimo

DenesL

unread,
Sep 4, 2008, 9:34:51 AM9/4/08
to web2py Web Framework
But gluon/sqlhtml.py has to be modified for it to work, if you are
going to include it in web2py I can do the mod. No point on writing an
alterego if it can not be used.

Massimo Di Pierro

unread,
Sep 4, 2008, 9:34:56 AM9/4/08
to web...@googlegroups.com
Sorry, misread point 3. Could you send me a patch so that I can read
the modification?

Massimo

DenesL

unread,
Sep 4, 2008, 1:54:02 PM9/4/08
to web2py Web Framework
ASAP

DenesL

unread,
Sep 5, 2008, 12:40:34 AM9/5/08
to web2py Web Framework
The sqlhtml.py mod is easy but then if IS_IN_SET_OR is used in
combination with IS_IN_DB it blows up in function sql_represent of
sql.py, whereas IS_NULL_OR has no problem.
Hmmmm....

Massimo Di Pierro

unread,
Sep 5, 2008, 1:03:01 AM9/5/08
to web...@googlegroups.com
The ql.py should be completely transparent to validators,
sql_represent in particular.

Validators are called only by FORM extended by SQLFORM sqlhtml.py.
The issue may be that IS_NULL_OR exposes options(self). In your case
that is more complex because you have two sets and you have to merge
them.

DenesL

unread,
Sep 5, 2008, 9:58:02 AM9/5/08
to web2py Web Framework
Oops, there was an invalid value in the test set.
I am sending you the patch files.

DenesL

unread,
Sep 9, 2008, 6:24:34 PM9/9/08
to web2py Web Framework
Massimo, did you receive the patch files?

mdipierro

unread,
Sep 9, 2008, 6:26:57 PM9/9/08
to web2py Web Framework
Can you please resend. I am not sure.

DenesL

unread,
Sep 9, 2008, 10:46:17 PM9/9/08
to web2py Web Framework
Files resent, did you get them?.
Reply all
Reply to author
Forward
0 new messages