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, 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.)
http://stackoverflow.com/questions/7376787/howto-customize-dbor-form-validator-in-web2py/7378577#7378577
Anthony
On Sunday, September 11, 2011 3:00:45 AM UTC-4, minhcd wrote:
>
> 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, folks!
>