pre selected checkboxes

757 views
Skip to first unread message

Fabiano Faver

unread,
Jul 10, 2012, 2:39:44 PM7/10/12
to web...@googlegroups.com
I want to creat something similar to SQLFORM(auth_membership) but I want the user to choose a group and all users show up as checkboxes and users who already belong to this group are checked as well.

I`m downt know how to pre-select these auth_users.

any tips?

Richard Vézina

unread,
Jul 10, 2012, 3:46:48 PM7/10/12
to web...@googlegroups.com
in your model Field(..., default=True) or in the controller db.table.field.default=True

You also need to use checkbox widget.

Richard

Fabiano Faver

unread,
Jul 10, 2012, 4:03:58 PM7/10/12
to web...@googlegroups.com
Won`t it apply True to all of them? 

this is what I have now with sqlform:
Field('users', requires = IS_IN_DB(db, db.auth_user, '%(email)s', multiple=True), widget=SQLFORM.widgets.checkboxes.widget )

I imagined there were a argument using  lambda that would set 'checked'  for each checkbox  that match the lambda, something like:

Field('users', requires = IS_IN_DB(db, db.auth_user, '%(email)s', multiple=True), widget=SQLFORM.widgets.checkboxes.widget, test_checked= lambda row: test_to_set_checked_for_each_checkbox )

I'm trying to use 2 FORMs now..

Richard Vézina

unread,
Jul 10, 2012, 4:10:38 PM7/10/12
to web...@googlegroups.com
default=lambda row: row.check_box_field

Richard

Fabiano Faver

unread,
Jul 10, 2012, 4:49:40 PM7/10/12
to web...@googlegroups.com
controller:

def gerenciar_grupos():
form_not_usu = SQLFORM.factory(
Field('usuarios', requires=IS_IN_DB(db, db.auth_user,'%(email)s',multiple=True),widget=SQLFORM.widgets.checkboxes.widget, default=True),
                                )
return dict(  form_not_usu = form_not_usu)


view:

{{extend 'layout.html'}}
{{=form_not_usu.custom.begin}}

<ul>
{{for x in form_not_usu.custom.widget.usuarios:}}
{{=LI(x)}}

{{pass}}
</ul>

{{=form_not_usu.custom.submit}}
{{=form_not_usu.custom.end}}



Checkboxes are not checked when accessing the page.

Em terça-feira, 10 de julho de 2012 16h46min48s UTC-3, Richard escreveu:

villas

unread,
Jul 11, 2012, 6:35:46 AM7/11/12
to web...@googlegroups.com
Don't you need a list field type for 'usuarios'?

Fabiano Faver

unread,
Jul 11, 2012, 1:50:49 PM7/11/12
to web...@googlegroups.com
the field is shown if I dont declare the type, however the checks are not checked with default=True, like I wrote

Richard Vézina

unread,
Jul 11, 2012, 3:52:33 PM7/11/12
to web...@googlegroups.com
Fabiano,

I think there is an issue with widget=SQLFORM.widgets.checkboxes.widget do you use it?

I try without widget=SQLFORM.widgets.checkboxes.widget and I can set default=True and I get a checked box, but if I use it the box doesn't get checked.

If you don't need the fancy feature of widget (I think that it is mostly to have many checkboxes for the same field and specify labels for those multiple checkboxes) avoid using it could solve your issue.

I will investigate further why and how SQLFORM.widgets.checkboxes.widget could be broken.

Richard

Richard Vézina

unread,
Jul 11, 2012, 4:19:54 PM7/11/12
to web...@googlegroups.com
Ok Fabiano, no issue with SQLFORM.widgets.checkboxes.widget...

I didn't know exactly how it works :)

In this thread look for Bruno's answers that how I figure out how to pass the proper default to widget :

Basically, since there is multiple checkboxes you need to define a IS_IN_SET requires with your options set (your check boxes) and then you can define a default by passing to default the label of what you use as label for the differents checkboxe in your defined set.

Here a example :

    form = SQLFORM.factory(
        Field('test', type='boolean', requires=IS_IN_SET([1,2,3], multiple=True), widget=SQLFORM.widgets.checkboxes.widget, default = 3),

I pass to default "3" since that the box that I want to be ckecked by default. 

In order to pass more then one choice you just pass a list to default like this :

    form = SQLFORM.factory(
        Field('test', type='boolean', requires=IS_IN_SET([1,2,3], multiple=True), widget=SQLFORM.widgets.checkboxes.widget, default = [2,3]),
        formstyle='divs'
        )

Hope it helps...

I appreciate answer your question since it helps me understand something I will have to cover in the coming days for a particular form.

Ciao!

Richard
Reply all
Reply to author
Forward
0 new messages