我有一个塔式应用程序,并且正在使用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>

但是,我似乎无法弄清楚如何验证这些字段。这是我的模式的相关条目

yardage = formencode.ForEach(formencode.validators.Int())

我试图验证这些字段中的每个字段都是一个INT。但是,这些字段没有发生验证。

更新如下所示,是该控制器操作的代码。我知道它在起作用,因为我可以验证其他表单字段。

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

更新有人建议我更改 yardage[]yardage没有结果。它们都应该是INT,但是将F放入其中一个元素并不会导致其无效。正如我之前说的,我能够验证其他表单字段。以下是我的整个模式。

import formencode

class 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. 。然后我会使用 FORMENCODE.VARIABLEDECODE 将其变成词典。这是在

模式:

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

蜂窝将验证 hole-#.parhole-#.yardage 都是int,不是空的。 formencode.ForEach 允许我申请 HoleSchema 到我通过的词典 variable_decode=True@validate 装饰师。

这里是 submit 我的动作

控制器:

@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 非常重要,否则字典将无法正确创建。

其他提示

c.form_result = schema.to_python(request.params) - (without dict)

似乎有效。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top