- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Pylons 应用程序,并使用 FormEncode 和 HtmlFill 来处理我的表单。我的模板 (Mako) 中有一组文本字段
<tr> <td>Yardage</td> <td>${h.text('yardage[]', maxlength=3, size=3)}</td> <td>${h.text('yardage[]', maxlength=3, size=3)}</td> <td>${h.text('yardage[]', maxlength=3, size=3)}</td> <td>${h.text('yardage[]', maxlength=3, size=3)}</td> <td>${h.text('yardage[]', maxlength=3, size=3)}</td> <td>${h.text('yardage[]', maxlength=3, size=3)}</td> <td>${h.text('yardage[]', maxlength=3, size=3)}</td> <td>${h.text('yardage[]', maxlength=3, size=3)}</td> <td>${h.text('yardage[]', maxlength=3, size=3)}</td> </tr>
但是,我似乎不知道如何验证这些字段。这是我的架构中的相关条目
码数 = formencode.ForEach(formencode.validators.Int())
我正在尝试验证每个字段都是 Int。但是,这些字段不会进行验证。
更新这里要求的是该 Controller 的操作代码。我知道它正在工作,因为我可以验证其他表单字段。
def submit(self): schema = CourseForm() try: c.form_result = schema.to_python(dict(request.params)) except formencode.Invalid, error: c.form_result = error.value c.form_errors = error.error_dict or {} c.heading = 'Add a course' html = render('/derived/course/add.html') return htmlfill.render( html, defaults = c.form_result, errors = c.form_errors ) else: h.redirect_to(controler='course', action='view')
更新IRC 上建议我将元素名称从 yardage[]
更改为 yardage
没有结果。它们都应该是整数,但将 f 放入其中一个元素不会导致其无效。正如我之前所说,我能够验证其他表单字段。下面是我的整个架构。
import formencodeclass CourseForm(formencode.Schema): allow_extra_fields = True filter_extra_fields = True name = formencode.validators.NotEmpty(messages={'empty': 'Name must not be empty'}) par = formencode.ForEach(formencode.validators.Int()) yardage = formencode.ForEach(formencode.validators.Int())
最佳答案
事实证明我想做的事情不太正确。
模板:
<tr>
<td>Yardage</td>
% for hole in range(9):
<td>${h.text('hole-%s.yardage'%(hole), maxlength=3, size=3)}</td>
% endfor
</tr>
(应该在开始时将其放入循环中。)您会注意到第一个元素的名称将变为 hole-1.yardage
。然后我将使用<a href="http://www.formencode.org/en/latest/modules/variabledecode.html" rel="noreferrer noopener nofollow">FormEncode.variabledecode</a>
把它变成一本字典。这是在
架构:
import formencode
class HoleSchema(formencode.Schema):
allow_extra_fields = False
yardage = formencode.validators.Int(not_empty=True)
par = formencode.validators.Int(not_empty=True)
class CourseForm(formencode.Schema):
allow_extra_fields = True
filter_extra_fields = True
name = formencode.validators.NotEmpty(messages={'empty': 'Name must not be empty'})
hole = formencode.ForEach(HoleSchema())
HoleSchema 将验证 hole-#.par
和hole-#.yardage
都是整数并且不为空。 formencode.ForEach
允许我申请HoleSchema
到我通过的字典variable_decode=True
到@validate
装饰器。
这是 submit
我的行动
Controller :
@validate(schema=CourseForm(), form='add', post_only=False, on_get=True,
auto_error_formatter=custom_formatter,
variable_decode=True)
def submit(self):
# Do whatever here.
return 'Submitted!'
使用@validate
装饰器允许以更简洁的方式验证和填写表单。 variable_decode=True
非常重要,否则字典将无法正确创建。
关于python - Pylons FormEncode 带有表单元素数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/994460/
我有一个 Pylons 应用程序,并使用 FormEncode 和 HtmlFill 来处理我的表单。我的模板 (Mako) 中有一组文本字段 Yardage ${h.text('y
我正在尝试做一些我认为应该很简单的事情,但我在让它工作时遇到了问题。这是我现在所拥有的,它按预期工作。 some_schema = Schema( multiples = ForEach(Uni
例如,我们以一个用户 Schema 为例,其中站点管理员设置请求的电话号码的数量: class MySchema(Schema): name = validators.String(not_e
我在 webapi (.net core 2.1) 中遇到绑定(bind)问题。通常,当我使用 xml/json 时,我可以向模型 props 添加属性(通过 XmlElement 或 JsonPro
我想使用 formencode 在我的 Pyramid 应用程序中验证上传文件的大小。据我了解,我需要创建一个从 formencode.validators.FormValidator) 继承的类并将
我正在开发一个基于 Pylons 的网络应用程序。因为我很清醒,所以我使用 jQuery(和插件)而不是编写原始 JavaScript。我也在使用 FormEncode验证我的应用程序的表单(尤其是新
我的问题可能与此相同,但建议的答案似乎没有帮助(或者我没有正确理解):Pylons FormEncode @validate decorator pass parameters into re-ren
我正在使用 mako 在 pylons 中工作,我想使用 FormEncode 为我的应用程序的多个部分创建表单和验证。我似乎找不到整个过程的任何好的例子。我的问题有两个: 技术 FancyValid
HTML: Controller : class myvalidator(formencode.Schema): allow_extra_fields = True filter
我正在使用formencode用于验证和提交我的 Pylons 应用程序中的表单。文档说它也可以用于生成表单,但没有任何示例。我什至找到了旧主题,说可以用 来完成 form = HTMLForm(fo
我正在使用 Pyramid 和 FormEncode 来尝试创建和验证地址列表。我正在使用 pyramid_simpleform 并一直在查看本教程 http://jimmyg.org/blog/20
我知道我可以在 python shell 中运行以下代码: import formencode ne = formencode.validators.NotEmpty() formencode.api
我是一名优秀的程序员,十分优秀!