Oh boy. And it just keeps growing hair -- in this case to spoof the
"options" portion of the validator. Well, this worked for me...
class OR(Validator):
def __init__(self, first, second, multiple=False):
self.first = first if isinstance(first,(list,tuple)) else [first]
self.second = second if isinstance(second,(list,tuple)) else
[second]
self.options = False
self.multiple = False
for ea in self.first:
if hasattr(ea,'options'):
self.options = ea.options
self.multiple = ea.multiple
break
for ea in self.second:
if hasattr(ea,'options'):
self.options = ea.options
self.multiple = ea.multiple
break
def __call__(self, value):
for ea in self.first:
retval,reterr = ea(value)
if reterr is not None:
break
if reterr is not None:
for ea in self.second:
retval,reterr = ea(value)
if reterr is not None:
break
return (retval, reterr)
On Monday, April 10, 2017 at 3:41:36 PM UTC-7, Joe Barnhart wrote:
>
> Well, I'm not sure there's a nice general solution to this one. Since
> "show_if" is kind of an add-on to Field and isn't really a part of
> Expression or Validator or anything else. Here's what I came up with...
>
> First I created my set of "show_if" fields and set their validators to the
> existing ones with a new "OR" validator added:
>
> expr = IS_EXPR('use_qual_times==True',environment=current.request.vars)
> for ea in ['qual_course','standard1','standard2','standard3']:
> field = table[ea]
> field.show_if = (table.use_qual_times==True)
> requires = field.requires
> if requires:
> field.requires = OR(expr, requires)
>
> Then I created a validator "OR" that takes a "first" and "second"
> argument. If "first" passes then "second" is never evaluated. I also
> allowed for "second" being a list of validators. But not "first", since
> I'm lazy and I knew it would only be the IS_EXPR validator I was adding:
>
> from gluon.validators import Validator
> class OR(Validator):
> def __init__(self, first, second):
> self.first, self.second = first, second
> if not isinstance(second, (list,tuple)):
> self.second = [second]
>
> def __call__(self, value):
> retval, reterr = self.first(value)
> if reterr is not None:
> for ea in self.second:
> retval,reterr = ea(value)
> if reterr is not None:
> break
> return (retval, reterr)
>
> Not as general case as I'd like. Mostly because I see no graceful way to
> automatically generate the IS_EXPR() from the argument supplied to show_if.
> If that could be automated, then this processing could be pushed into the
> FORM or SQLFORM mechanics. (But it's getting a little crowded in there,
> I'll admit!)
>
> -- Joe
>
> P.S. I can't stand being a lazy slug so I fixed it:
>
> from gluon.validators import Validator
> class OR(Validator):
> def __init__(self, first, second):
> self.first = first if isinstance(first,(list,tuple)) else [first]
> self.second = second if isinstance(second,(list,tuple)) else
> [second]
>
> def __call__(self, value):
> for ea in self.first:
> retval,reterr = ea(value)
> if reterr is not None:
> break
> if reterr is not None:
> for ea in self.second:
> retval,reterr = ea(value)
> if reterr is not None:
> break
> return (retval, reterr)
>
>
>
> On Monday, April 10, 2017 at 11:23:37 AM UTC-7, Anthony wrote:
>>
>> This might be as simple as emptying out the "requires" attribute if the
>>> "show_if" causes the field to be visible. But I don't change core code --
>>> I don't want the hassle of departing from the stock distro of web2py.
>>>
>>
>> You could change the core code and submit a pull request. ;-)
>>
>>
>>
>>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
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/d/optout.