Conditional Fields & Validators

51 views
Skip to first unread message

James O' Driscoll

unread,
Dec 1, 2020, 9:35:15 PM12/1/20
to web2py-users
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

James O' Driscoll

unread,
Dec 3, 2020, 2:05:55 AM12/3/20
to web2py-users
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

Leonel Câmara

unread,
Dec 4, 2020, 7:56:54 AM12/4/20
to web2py-users
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) 

James O' Driscoll

unread,
Dec 4, 2020, 8:26:40 AM12/4/20
to web...@googlegroups.com
Hey mate,

I got around that specific validator,  however the is_in_set validator causes the problem. (It’s needed to create the select field in HTML) 

I managed to get around it with onvalidation, doing the validation based on a state of the control but this is adding more logic on the backend and takes away from the model functionality.  

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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/fiHjfVb01Z8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/2ddf8a1a-2ebe-4ef9-a0e9-6fd5f6c2c26an%40googlegroups.com.

James O' Driscoll

unread,
Dec 6, 2020, 8:33:24 PM12/6/20
to web2py-users
I had to remove the use of show_if and instead implement custom JS to show/hide fields based on the boolean field.  With onvalidation function being used to preform validation.

Not sure that the show_if feature is properly implemented with regard to the validation system.  

Regards,
James
Reply all
Reply to author
Forward
0 new messages