Initializing a form with ToscaWidgets

Question:

How can I pre-populate CheckBoxTable from ToscaWidgets with values.

Background:

I have looked everywhere and I cannot figure out how to initialize a particular form field using ToscaWidgets. Most form fields seem to respond well to initialization, for example, if I create a form with one TextField in it, when I render the form in the template and pass to fieldValue = x, where fieldValue is the name of TextField and x is some TextField line will be filled with x. My problem is with the entire multiple select field, in particular with CheckBoxTable. No matter what I pass, it will not initialize multiple selections. Here is an example of what I'm saying is a user edit page using CheckBoxTable for groups, so you can select multiple groups or not from a list of several groups extracted from the database:

What I have:

My widget:

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:

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}

:

UserAttributs :

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

:

, , CheckBoxTable. CheckBoxTable , , , , . , , , , - , CheckBoxTable.

:

Turbogears 2 ToscaWidgets 0.9.7 Mako .

+3
1

param.

import tw.forms
f = tw.forms.TableForm(fields=[tw.forms.CheckBoxTable("name",options=(("foo"),("bar")))]) 
f(value={"name":{"foo":True,"bar":False}})
>>> u'<form xmlns="http://www.w3.org/1999/xhtml" action="" method="post" class="tableform">\n    <table border="0" cellspacing="0" cellpadding="2">\n<tr id="name.container" class="even" title="">\n            <td class="labelcol">\n                <label id="name.label" for="name" class="fieldlabel">Name</label>\n            </td>\n            <td class="fieldcol">\n                <table id="name" class="checkboxtable">\n    <tbody>\n    <tr>\n        <td>\n
    <input id="name_0" value="foo" name="name" type="checkbox" checked="checked" />\n            <label for="name_0">foo</label>\n        </td>\n    </tr><tr>\n        <td>\n            <input id="name_1" value="bar" name="name" type="checkbox" />\n            <label for="name_1">bar</label>\n        </td>\n    </tr>\n
</tbody>\n</table>\n            </td>\n        </tr><tr id="submit.container" class="odd" title="">\n            <td class="labelcol">\n            </td>\n
       <td class="fieldcol">\n                <input type="submit" class="submitbutton" value="Submit" />\n            </td>\n        </tr>\n    </table>\n</form>'
+1

Source: https://habr.com/ru/post/1711699/


All Articles