如何让 Pylons formencode 验证器忽略某些字段
我有一个表单,其中有一些输入文本框和一些选择框。我的验证在文本框上运行得很好。我不在乎是否有任何选择框保留默认值,但每次我提交表单时,它都会进入塔架错误页面,显示“无效:请输入...的值”,但我不希望这样发生。
这是我的验证器函数:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的
formencode.validators
方法中:not_empty=False
。参数 not_empty false 就是你所说的:空是一个有效值。if_missing = None
,这会将结果设置为None
。On your
formencode.validators
methods: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_missing = None
which will set your result toNone
.我认为你应该添加以下行到它
filter_extra_fields = True
I think you should add the following line to it
filter_extra_fields = True