IS_NOT_ID_DB for mutliple fields

28 views
Skip to first unread message

Yebach

unread,
Jan 21, 2017, 3:42:42 AM1/21/17
to web2py-users
Hello

I have to make a validator on my SQLFORM that checks 4 fields

my table is


db.define_table('workers_skills',
Field('ws_organisation', type ='integer'),
Field('ws_worker', 'reference workers', label = T('Worker')),
Field('ws_skill', 'reference skills', label = T('Skill')),
Field('ws_priority', type ='integer', label = T('Priority')),
Field ('ws_status', type= 'integer', label = T('Status'), widget = SQLFORM.widgets.options.widget, default = 1),
migrate = settings.migrate)

The check is on fields ws_organisation, ws_worker, ws_skill and ws_priority

So a worker cannot have the same ws_priority value for two different skills. 

this are the validators already set

db.workers_skills.ws_priority.requires = [IS_NOT_EMPTY(error_message=T('Missing priority level')),
IS_INT_IN_RANGE(1, 100000,error_message=T('Priority cannot be 0 or larger then 100000!'))

db.workers_skills.ws_worker.requires = IS_IN_DB(
db((db.workers.w_organisation == org) & (db.workers.w_status < 100)),
db.workers.id, '%(w_nick_name)s - %(w_last_name)s %(w_first_name)s', zero=T('Select worker'))

#preprečmo da bi ble podvojene vrednosti isti delavc isti skill pa da je dropdown mani sk_name glede na organizacijo
db.workers_skills.ws_skill.requires = IS_IN_DB(db((db.skills.sk_organisation == org) &
(db.skills.sk_status < 100)), db.skills.id, '%(sk_name)s',
zero=T('Select skill'),
_and=IS_NOT_IN_DB(db(db.workers_skills.ws_worker == request.vars.ws_worker),
'workers_skills.ws_skill'))

What would be the most elegant way to solve this?

thank you

Massimo Di Pierro

unread,
Jan 27, 2017, 11:34:07 PM1/27/17
to web2py-users
how about this?

def apply_rules(form):
    if does not pass your criteria:
       form.errors[somefield] = 'some message'
form = SQLFORM(db.workers_skills).process(onvalidation=apply_rules)
Reply all
Reply to author
Forward
0 new messages