Forum OpenACS Q&A: How do I selectively position some checkboxes as 'checked' in a form ?

The old way of doing thing would use a db_foreach with a tailor-made query to selectively check some boxes in a form, based on some value in the DB.

Example :

db_foreach all_users_q {
select nom, id,
   case when exists (select 1 from map_table
     where user_id=id and serv_id=1)
   then 'checked' else '' end as checked
from users_table
} {
ns_write "$nom <input type=checkbox $checked
 name=user value=$id>"

With the new way, I am supposed to use some template::element create, but I can't find a way to specify the -options so that some boxes are checked, but not all.

I have read whatever documentation on templating that I could find, to no avail. (Yes, I have tried many variations on template::element set_properties ...)

Anybody care to help me out of this one ? I feel bad : I am being punished for wanting to do things the right way !

element create foo bar -widget checkbox -options { { box1 "Box 1" } { box2 "Box 2" } } -values { box1 }

Above should have box1 checked, box2 not checked.

It's written from memory, so I may have switched some arguments.

Basically: -values switch takes a list of option values for which the checkboxes should be checked.

Solution :

Lars' solution works. Here is the correct code snippet :

template::element create mod_serv users 
    -widget checkbox 
    -options { {Premier 1} {Deuz 2} {Troiz 3}} 
    -values { 1 2 }
And it should probably also work (I haven't tested it yet) with template::element set_value...

Thanks, Lars !