使用 ToscaWidgets 进行表单初始化

发布于 2024-07-25 21:21:53 字数 2939 浏览 4 评论 0原文

问题

如何在 ToscaWidgets 中预填充 CheckBoxTable 值。

背景:

我到处都看过,但我似乎不知道如何使用 ToscaWidgets 初始化特定的表单字段。 大多数表单字段似乎对初始化响应良好,就像当我在模板中渲染表单并传入 fieldValue=x 时创建一个包含单个 TextField 的表单时,其中 fieldValue 是 TextField 的名称,x 是某个字符串TextField 将填充 x。 我的问题是所有多选字段,特别是 CheckBoxTable。 无论我传入什么,它都不会初始化多重选择。 这是我正在谈论的一个示例,它是一个带有用于组的 CheckBoxTable 的用户编辑页面,因此您可以从从数据库获取的多个组的列表中选择多个组或不选择任何组:

我拥有的:< /strong>

我的小部件是:

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")

在我的控制器中我有:

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')

在我的模板(Mako)中我有:

${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

我尝试过的get:

所有这些代码的结果是一个用户编辑表单,其中的数据预先填充了除 CheckBoxTable 之外的用户数据。 CheckBoxTable 的数据库中的所有组都显示为空,这是我需要显示的组,但用户所在的组已被选中。 我认为模型属性中的代码会执行此操作,因为这就是它对每个其他字段执行的操作,但我肯定缺少一些关于 CheckBoxTable 实例化的基本内容。

规格

我使用 Turbogears 2 和 ToscaWidgets 0.9.7 表单以及 Mako 进行模板化。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-08-01 21:21:53

通过值参数设置它们。

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>'

set them via the value 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>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文