带有多个复选框的 Pylons/Formencode
今天,我在验证多个复选框时遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能使用
formencode.validators.Set
:maybe using
formencode.validators.Set
:redrockettt,
您看过变量解码的文档字符串吗?它建议您使用类似的内容:
查看variabledecode.py中的文本,或粘贴 这里。
redrockettt,
Have you looked at the docstring to variabledecode? It suggests you use something like:
Check out the text in variabledecode.py, or pasted here.