Is it possible to change the boolean field widget from checkbox to option with yes and no?

115 views
Skip to first unread message

Jeff Plata

unread,
Feb 26, 2023, 8:51:30 AM2/26/23
to web2py-users
Is it possible to change the boolean field widget from checkbox to option with yes and no? How?

Dave S

unread,
Mar 29, 2023, 7:10:19 AM3/29/23
to web2py-users
On Sunday, February 26, 2023 at 5:51:30 AM UTC-8 jeff...@gmail.com wrote:
Is it possible to change the boolean field widget from checkbox to option with yes and no? How?


I don't think you can do that with the default radio widget.

This works:

    form3 = SQLFORM.factory(
        Field('bool', type = "string", default = False, \
              requires=IS_IN_SET(['yes', 'no']), \
              widget = lambda field, value:  SQLFORM.widgets.radio.widget(field, value)),
        _name = "Georgeform")
    if form3.process(formname = "Georgeform", dbio = False, keepvalues = True).accepted:
        response.flash = "form3 bool %s" % ("T" if form3.vars.bool == 'yes' else 'F')
    else:
        response.flash = "form3 bool not set"


but if you set the field type to  boolean, you always get form3.vars.bool = True.

Since I'm about to go to bed (as I said an hour ago), I'm not going to explore custom widgets, or using the INPUT() helper  But synthesizing a boolean from the radio set is my idea of an obvious approach.  The caveat is that if you're using SQLFORM instead of the factory, I'm not sure what will happen with the row posted to the table.  Again, not before bed.

Good luck!

/dps

Dave S

unread,
Mar 29, 2023, 7:46:26 AM3/29/23
to web2py-users
On Wednesday, March 29, 2023 at 4:10:19 AM UTC-7 Dave S wrote:
On Sunday, February 26, 2023 at 5:51:30 AM UTC-8 jeff...@gmail.com wrote:
Is it possible to change the boolean field widget from checkbox to option with yes and no? How?


I don't think you can do that with the default radio widget.

This works:

    form3 = SQLFORM.factory(
        Field('bool', type = "string", default = False, \

um, that default should now be 'yes' or 'no', since the type isn't boolean

Dave S

unread,
Mar 29, 2023, 8:18:49 AM3/29/23
to web2py-users
Ok,  I won't try anything before bed, but I'd probably make the table def use a computed field for the boolean and have it not visible, and just add the radio set  field to the table.

/dps

Leonel Câmara

unread,
Mar 29, 2023, 8:50:09 AM3/29/23
to web2py-users
You could make a custom widget for that field, in your define_table one of the attributes a Field can receive is widget.

However for such a simple case it might be worth it so simply use something like: https://github.com/gitbrent/bootstrap4-toggle/

Jeff Plata

unread,
Apr 4, 2023, 4:33:20 AM4/4/23
to web2py-users
Thank you all for your suggestions. In the end, I settled for a string field with IS_IN_SET(['yes', 'no']).

Bootstrap toggle is nice, but the alignment is off, and the stubborn label is still on the right where I don't want it to be. I know these issues are fixable, but nah, I'm going for the quick way.
Message has been deleted
Message has been deleted

Jeff Plata

unread,
Apr 5, 2023, 12:57:57 AM4/5/23
to web2py-users
After further tests, this model worked for me eventually:

    Field("is_active", "boolean", label="Active", requires=IS_IN_SET([(True,'yes'), (False,'no')], zero=None),
        default=(True,'yes'), widget=SQLFORM.widgets.options.widget)

and on the controller....

def validate_library(form):
    form.vars.is_active = True if form.vars.is_active in ['yes','True'] else False

@auth.requires_login()
def library():
    grid = SQLFORM.grid(db.service, formname='grid_services', create=True, csv=False, paginate=20,
            searchable=True, editable=True, deletable=False, onvalidation=validate_library)
    return dict(grid=grid)
Reply all
Reply to author
Forward
0 new messages