How to use validators on 2 fields?

18 views
Skip to first unread message

tititi

unread,
Aug 31, 2009, 11:10:48 AM8/31/09
to web2py-users
Hi,

I'm looking at IS_IN_DB and IS_NOT_IN_DB to filter out inserts and
want to know if it's possible to use 2 fields to validate an insert to
prevent duplicates. For example, let's say I want emails to be unique
in email field, now I want it to be email AND postalcode to be unique.
I would like to use this at the model level and not a controller, is
this possible?

mdipierro

unread,
Aug 31, 2009, 1:59:01 PM8/31/09
to web2py-users
Say you have two fields a and b and you want a+b to be unique. You
always validate one at the time because only one can report an error.
Therefore you you want to validate b so that a+b is unique:

db.table.b.requires=IS_NOT_IN_DB(db
(db.table.a==request.vars.a),'table.b')

tititi

unread,
Sep 4, 2009, 6:32:50 AM9/4/09
to web2py-users
Thanks for the response Massimo!

Let's say I have 2 fields (table.user_id and table.postal_code) in a
many to many table. And this would be the controller level logic:

myduplicate = (db.table.user_id!= request.args[0]) &
(table.postal_code!= request.args[1]).select().[0]

So it's possible to have user A to have more than 1 postal code and
same postal code have more than 1 user. So what I don't want is to
duplicate an insert for the same thing (say user A with 01234 two
times). User A cannot enter the same postal codes 2 times. How do I
write this logic at the model level?


On Aug 31, 7:59 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Say you have two fields a and b and you want a+b to be unique. You
> alwaysvalidateone at the time because only one can report an error.
> Therefore you you want tovalidateb so that a+b is unique:
>
> db.table.b.requires=IS_NOT_IN_DB(db
> (db.table.a==request.vars.a),'table.b')
>
> On Aug 31, 10:10 am, tititi <briant...@gmail.com> wrote:
>
>
>
> > Hi,
>
> > I'm looking at IS_IN_DB and IS_NOT_IN_DB to filter out inserts and
> > want to know if it's possible to use 2 fields tovalidatean insert to
Reply all
Reply to author
Forward
0 new messages