如何让 Pylons formencode 验证器忽略某些字段

发布于 2024-11-04 03:16:48 字数 983 浏览 0 评论 0原文

我有一个表单,其中有一些输入文本框和一些选择框。我的验证在文本框上运行得很好。我不在乎是否有任何选择框保留默认值,但每次我提交表单时,它都会进入塔架错误页面,显示“无效:请输入...的值”,但我不希望这样发生。

这是我的验证器函数:

class Registration(formencode.Schema):
    allow_extra_fields=True
    first_name = formencode.validators.String(strip=True, not_empty=True)
    last_name = formencode.validators.String(strip=True, not_empty=True)
    address = formencode.validators.String(strip=True, not_empty=True)
    phone = formencode.validators.PhoneNumber(strip=True, not_empty=True)
    email = formencode.validators.Email(resolve_domain=True, not_empty=True)
    messages = {
        'tooLow': "You must be at least 18 in order to register for an event.",
        'tooHigh': "You obviously are not that old.  Please enter your actual age.",
    }
    age = formencode.validators.Int(min=18, max=999, not_empty=True, messages=messages)

我认为使用allow_extra_fields = True,如果留空/默认,它将允许函数中未提供的表单中的字段通过。我选择了应忽略的名为“听到”、“频率”和“级别”的框。

任何帮助将不胜感激。

I have a form that has some input text boxes and well as some select boxes. I have validation working perfectly on the textboxes. I don't care if any of the select boxes are left defualt or not, but everytime I submit the form it goes to a pylons error page saying "Invalid: Please enter value for ..." But I don't want that to happen.

here is my validator function:

class Registration(formencode.Schema):
    allow_extra_fields=True
    first_name = formencode.validators.String(strip=True, not_empty=True)
    last_name = formencode.validators.String(strip=True, not_empty=True)
    address = formencode.validators.String(strip=True, not_empty=True)
    phone = formencode.validators.PhoneNumber(strip=True, not_empty=True)
    email = formencode.validators.Email(resolve_domain=True, not_empty=True)
    messages = {
        'tooLow': "You must be at least 18 in order to register for an event.",
        'tooHigh': "You obviously are not that old.  Please enter your actual age.",
    }
    age = formencode.validators.Int(min=18, max=999, not_empty=True, messages=messages)

I thought with allow_extra_fields = True it would allow for fields in the form that aren't supplied in the function to pass if left blank/default. I have select boxes named heard, frequency, and level that should be ignored.

Any help here would be greatly appreciated.

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

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

发布评论

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

评论(2

失退 2024-11-11 03:16:48

在您的 formencode.validators 方法中:

  • 如果您的表单提交字段且值为空字符串,请使用 not_empty=False。参数 not_empty false 就是你所说的:空是一个有效值。
  • 如果字段不存在,则使用 if_missing = None ,这会将结果设置为 None

On your formencode.validators methods:

  • Use not_empty=False if your form is submitting the fields and the values are empty strings. The param not_empty false is how you say: empty is a valid value.
  • If the fields do not exist then use if_missing = None which will set your result to None.
空城缀染半城烟沙 2024-11-11 03:16:48

我认为你应该添加以下行到它

filter_extra_fields = True

I think you should add the following line to it

filter_extra_fields = True

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