带有多个复选框的 Pylons/Formencode

发布于 2024-08-07 04:52:27 字数 841 浏览 5 评论 0原文

今天,我在验证多个复选框时遇到了 Pylons/Formencode 的一些问题。作为一点背景知识,我的 Mako 模板中有这样的内容:

<input type="checkbox" name="Project" value="1">Project 1</input>
<input type="checkbox" name="Project" value="2">Project 2</input>
<input type="checkbox" name="Project" value="3">Project 3</input>
<input type="checkbox" name="Project" value="4">Project 4</input>
<input type="checkbox" name="Project" value="5">Project 5</input>

在我的验证模式中,我有这样的内容(请原谅任何错误 - 我面前没有确切的代码):

Project = formencode.foreach.ForEach(formencode.validators.Int())

我期望得到一个列表选中项目的后面(听起来很合理,对吧?),但相反,尽管选中了所有框,但我还是得到了一个包含单个项目的列表。我做错了吗?或者我想要拿回的东西是否可能实现?我已经为每个复选框项目编写了一个 hack,其中包含 onclicks,将选中的项目附加到一个数组,然后以 JSON 格式发回 - 这很丑陋,也很痛苦,因为如果验证失败,我必须自己重新填充所有字段。

有人有什么想法吗?

I ran up against a few problems with Pylons/Formencode today when it came to validating multiple checkboxes. As a bit of background I have something like this in my Mako template:

<input type="checkbox" name="Project" value="1">Project 1</input>
<input type="checkbox" name="Project" value="2">Project 2</input>
<input type="checkbox" name="Project" value="3">Project 3</input>
<input type="checkbox" name="Project" value="4">Project 4</input>
<input type="checkbox" name="Project" value="5">Project 5</input>

In my validation schema I had something like this (please forgive any errors - I don't have the exact code infront of me):

Project = formencode.foreach.ForEach(formencode.validators.Int())

I was expecting to get a list back of checked items (sounds reasonable, right?) but instead I got a list with a single item despite having all boxes checked. Am I doing this wrong or is what I want to get back even possible? I have written a hack around it with onclicks for each checkbox item that appends the checked item to an array which is then posted back in JSON format - this is ugly and a pain since I have to repopulate all the fields myself if validation fails.

Anyone have any ideas?

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

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

发布评论

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

评论(2

绅士风度i 2024-08-14 04:52:27

可能使用 formencode.validators.Set :

>>> Set.to_python(None)
[]
>>> Set.to_python('this')
['this']
>>> Set.to_python(('this', 'that'))
['this', 'that']
>>> s = Set(use_set=True)
>>> s.to_python(None)
set([])
>>> s.to_python('this')
set(['this'])
>>> s.to_python(('this',))
set(['this'])

maybe using formencode.validators.Set:

>>> Set.to_python(None)
[]
>>> Set.to_python('this')
['this']
>>> Set.to_python(('this', 'that'))
['this', 'that']
>>> s = Set(use_set=True)
>>> s.to_python(None)
set([])
>>> s.to_python('this')
set(['this'])
>>> s.to_python(('this',))
set(['this'])
忘东忘西忘不掉你 2024-08-14 04:52:27

redrockettt,

您看过变量解码的文档字符串吗?它建议您使用类似的内容:

<input type="checkbox" name="Project-1" value="1">Project 1</input>
<input type="checkbox" name="Project-2" value="2">Project 2</input>
<input type="checkbox" name="Project-3" value="3">Project 3</input>

查看variabledecode.py中的文本,或粘贴 这里

redrockettt,

Have you looked at the docstring to variabledecode? It suggests you use something like:

<input type="checkbox" name="Project-1" value="1">Project 1</input>
<input type="checkbox" name="Project-2" value="2">Project 2</input>
<input type="checkbox" name="Project-3" value="3">Project 3</input>

Check out the text in variabledecode.py, or pasted here.

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