Locke
Locke

Reputation: 341

Howto customize DB(or Form) Validator in web2py?

Here are my 2 tables defintion:

db.define_table('question',
    Field('category_id',db.category),
    Field('content'),
    Field('number_of_options','integer'),
    Field('has_options_detail','boolean',default=False)) ## Has or not has detail for each option

db.define_table('options_detail', ## This table only for options that have detail explanation
    Field('question_id',db.question),
    Field('serial'), ## Option ordering 
    Field('detail')) ## Detail explanation of option

How can I restrict that: db.options_detail.question_id must belong to questions that have field "has_options_detail == True"

Thanks in advance!

Upvotes: 2

Views: 506

Answers (1)

Anthony
Anthony

Reputation: 25536

This should do it:

db.options_detail.question_id.requires = IS_IN_DB(
    db(db.question.has_options_detail==True), 'question.id')

Note, the IS_IN_DB validator can take a DAL Set object as the first argument (see here), which enables you to filter the referenced table based on any criteria.

(You could also specify the above requires argument directly in the Field() declaration.)

Upvotes: 2

Related Questions