You can change the field requires in the controller depending on whether the field should be shown doing something like this
def index(): db.purchase.coupon_code.show_if = (db.purchase.have_coupon==True) if request.vars.have_coupon: db.purchase.coupon_code.requires = IS_NOT_EMPTY() else: db.purchase.coupon_code.requires = IS_EMPTY() form = SQLFORM(db.purchase).process() return dict(form = form) db.purchase.coupon_code.show_if = (db.purchase.have_coupon==True) A quinta-feira, 3 de dezembro de 2020 à(s) 07:05:55 UTC, james.o...@gmail.com escreveu: > All, > > I am trying to get a solution to the above problem, I am using a > onvalidation function. If have_coupon is checked then I call IS_NOT_EMPTY > and set the form.errors.coupon_code manually. This is not ideal but it > works. > > However I cannot seem to get a work around for a dropdown input i.e. > IS_IN_SET as this is needed on the model to create the correct input but > inturn fails the processing of the form as onvalidation doesnot get called. > > Any help would be great. > > Regards, > James > > > On Wednesday, December 2, 2020 at 12:35:15 PM UTC+10 James O' Driscoll > wrote: > >> Hello all, >> >> I have a question regarding conditional fields and using validators. >> >> Take the example from the book with a slight change: >> >> db.define_table('purchase', Field('have_coupon', 'boolean'), >> Field('coupon_code', requires=IS_NOT_EMPTY())) >> >> with controller: >> >> def index(): >> db.purchase.coupon_code.show_if = (db.purchase.have_coupon==True) >> form = SQLFORM(db.purchase).process() >> return dict(form = form) >> >> show_if works well but if i need validators in place for when say >> have_coupon is true, to validate coupon_code I get an error when >> have_coupon is false as coupon_code is empty. >> >> I would prefer to keep the validator on the model, is there a way to >> accomplish this. >> >> Regards, >> James >> >> -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/2ddf8a1a-2ebe-4ef9-a0e9-6fd5f6c2c26an%40googlegroups.com.