Question:
How do I prefill a CheckBoxTable from ToscaWidgets with values.
Background:
I've looked everywhere and I can't seem to figure out how to initialize a particular form field with ToscaWidgets. Most form fields seem to respond just fine to initialization, like if I create a form with a single TextField in it when I render the form in the template and pass in fieldValue=x where fieldValue is the name of the TextField and x is some string the TextField will be filled with x. My problem is with all multiple select field, in particular CheckBoxTable. No matter what I pass in it will not initialize the multiple select. Here is an example of what I'm talking about, it's a user edit page with a CheckBoxTable for groups so you can select several or no groups out of a list of several groups fetched from the databse:
What I have:
My widget is:
from tw import forms
class UserForm(forms.TableForm):
show_errors = True
submit_text = "Create User"
clientOptions = [(-1, "Select a Client")]
groupOptions = [(-1, "Select a Group")]
fields = [forms.TextField('name', label_text='User Name', validator=String(not_empty=True), size=40),
forms.Spacer(),
forms.SingleSelectField('clientID', label_text='Client Name', validator=Int(min=0), options=clientOptions),
forms.Spacer(),
forms.CheckBoxTable('groups', lable_text='Groups', validator=Set(), options=groupOptions, num_cols=3),
forms.Spacer(),
forms.PasswordField('password', label_text="Password", validator=String(not_empty=True, min=6), size=40),
forms.PasswordField('passwordAgain', label_text="Repeat Password", validator=String(not_empty=True, min=6), size=40),
forms.HiddenField('id')]
editUserForm = UserForm("createUserForm", action='alterUser', submit_text="Edit User")
In my controller I have:
result = model.DBSession.query(model.User).filter_by(id=kw['id']).first()
tmpl_context.form = editUserForm
clientOptions=model.DBSession.query(model.Client.id, model.Client.name)
groupOptions=model.DBSession.query(model.Group.id, model.Group.name)
formChildArgs = dict(clientID=dict(options=clientOptions), groups=dict(options=groupOptions))
userAttributes=dict(id=result.id, name=result.name, groups=[g.id for g in result.groups], clientID=result.clientID, password=result.password, passwordAgain=result.password)
return dict(verb="Edit", modelName = "User", modelAttributes=userAttributes, formChildArgs=formChildArgs, page='editUser')
and in my template (Mako) I have:
${tmpl_context.form(modelAttributes, child_args=formChildArgs) | n}
What I've tried:
In my userAttributs dictionary I've tried:
groups=[g.id for g in result.groups]
groups=[g.name for g in result.groups]
groups=[(g.id, g.name) for g in result.groups]
groups=[[g.id, g.name) for g in result.groups]
groups=result.groups
What I get:
The result of all of this code is a User edit form with data pre-filled with the user data except for the CheckBoxTable. The CheckBoxTable has all of the groups in my database displaying and empty, what I need for for them to be displaying but have the groups the user is apart of checked. I thought the code in the model attributes would do this, since that's what it does for every other field, but there must be some fundamental thing I'm missing about CheckBoxTable instantiation.
Specs:
I'm using Turbogears 2 with ToscaWidgets 0.9.7 forms and Mako for templating.