如何在Deform / Colander中定义有权访问的自定义验证器 所有节点值。我需要访问两个字段中的值才能确定某个特定值是否有效?
答案 0 :(得分:3)
以下是场间验证的示例。 http://deformdemo.repoze.org/interfield/
答案 1 :(得分:3)
要为所有漏勺字段放置验证器,我们可以简单地执行此操作
验证方法:
def user_DoesExist(node,appstruct):
if DBSession.query(User).filter_by(username=appstruct['username']).count() > 0:
raise colander.Invalid(node, 'Username already exist.!!')
<强>架构:强>
class UserSchema(CSRFSchema):
username = colander.SchemaNode(colander.String(), description="Extension of the user")
name = colander.SchemaNode(colander.String(), description='Full name')
extension = colander.SchemaNode(colander.String(),description='Extension')
pin = colander.SchemaNode(colander.String(), description='PIN')
查看:强>
@view_config(route_name='add_user', permission='admin', renderer='add_user.mako')
def add_user(self):
#Here you can provide validator method as a parameter. And you can validate any field you want.
schema = UserSchema(validator = user_DoesExist).bind(request=self.request)
form = deform.Form(schema, action=self.request.route_url('add_user'), buttons=('Add User','Cancel'))
如果我的情况错了,请纠正我。
由于
答案 2 :(得分:2)
答案是:
def verify_email_validator(form, values):
if values['email_address'] != values['verify_email']:
raise Invalid(form, 'Email values do not match')
class MySchema(MappingSchema):
def __init__(self, *args, **kwargs):
super(KickEntrySchema, self).__init__(*args, **kwargs)
self.validator=verify_email_validator # entire form validator
email_address = SchemaNode(Email())
verify_email = SchemaNode(Email())
请注意,仅当没有单个字段验证器引发错误时,才会调用表单验证程序。