Validator IS_NOT_IN_DB not working | web2py

36 views
Skip to first unread message

Jay B

unread,
Oct 18, 2019, 4:57:39 AM10/18/19
to web2py-users
Hello guys,

I'm struggling with a problem that's driving me crazy.  The validator IS_NOT_IN_DB which I use for my object "tag" does not work for me.

# TAG
db
.define_table('tag',
               
Field('name', requires=(IS_SLUG(), IS_LOWER(), IS_NOT_IN_DB(db, 'tag.name'))),
               
Field('is_public', 'boolean', default=False, readable=False, writable=False),
                auth
.signature,
                common_filter
= lambda query: db.tag.is_public == True)
db
.tag.name.requires = IS_NOT_IN_DB(db, 'tag.name')

As you can see, there are two ways I made sure that no duplicates can be created, in the name field itself and outside of the table definition.

Registered user can suggest tags:





Although the tag "test" has already been submitted twice I could submit it a third time.


The function looks as follows:


What I already tried (without improvement):

- adding "unique=True" to the name field
- just using .process() without "onvalidation"
- "ignore_common_filters"  on and off
- inside the "my_form_processing"-method I checked if the amount of db objects with the given name is > 0 , but i could not figure out how to stop the form from processing

Please let me know how I can fix that problem.

Thanks!
Jay

Annet

unread,
Oct 18, 2019, 6:32:17 AM10/18/19
to web2py-users
What about


form = SQLFORM()
if form
.process(...).accepted


instead of

form = SQLFORM().process()
if form.accepted:


Best,
Annet

Jay B

unread,
Oct 18, 2019, 7:16:02 AM10/18/19
to web2py-users
Wow, that's actually how it works! Thank you very much!
Reply all
Reply to author
Forward
0 new messages