This is a bad design, but sometimes you can't do better... I wrote this in
the pass :

# If you use module to store this piece of code uncomment the line below
# from gluon.validators import is_empty
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',
                 _and=None,
                 ):
        self.others = others
        self.error_message = error_message
        self._and = _and

    def set_self_id(self, id):
        if self._and:
            self._and.record_id = id

    def __call__(self, value):
        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 (value, None)
        else:
            return (value, self.error_message)

Then you have to use this validators like this :



db.ref_tregistry.vregistry_id.requires = [IS_EMPTY_OR(IS_IN_DB(db,
'v_ref_vol_num.vregistry_id', '%(fna_vol)s',

 orderby=('fna_vol'),

 _and=IS_NOT_IN_DB(tom_entry_set,

     'ref_tregistry.vregistry_id',

     error_message=T('tome already exist')))
                                                      ),

ONLY_ONE_CAN_BE_FILLED([request.vars.fnaregistry_id],

 error_message=T('Pick a volume or an folder'))]

db.ref_tregistry.fnaregistry_id.requires = [IS_EMPTY_OR(IS_IN_DB(db,
'ref_fnaregistry.fnaregistry_id',
                                                                 lambda
row: '%02d-%02d-%03d %s' %

 (int(row.num_part1.rstrip()),

  int(row.num_part2.rstrip()),

  int(row.num_part3.rstrip()),

  row.fna_title.rstrip()),

 _and=IS_NOT_IN_DB(tom_entry_set,

       'ref_tregistry.fnaregistry_id',

error_message=T('tome already exist')))),

ONLY_ONE_CAN_BE_FILLED([request.vars.vregistry_id],

 error_message=T('Pick a volume or an folder'))]


It needs to be applied on both fields that have to be filled or not...

Richard

On Tue, Mar 24, 2015 at 4:49 PM, Phil Hughes <nica...@gmail.com> wrote:

> I have a record with two reference links -- to two different tables. The
> validation I want is that one and only one of the two references are filled
> in. Cleary I can just brute force this when processing the form but I
> wonder if I am missing a way to do this in the model.
>
> --
> 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 web2py+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to