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


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.

Reply via email to