本文介绍了可以访问所有节点的变形/考兰德验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Deform/Colander中定义可以访问的自定义验证器所有节点值.我需要从两个字段中访问值,以便确定特定值是否有效?
How do you define a custom validator in Deform/Colander that has access toall node values. I need to access the values from two fields in order to decide if a particular value is valid or not?
推荐答案
要为所有漏勺字段放置验证器,我们可以简单地做到这一点
To place a validator for all colander fields we can simply do this
验证器方法:
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'))
如果我的情况不对,请纠正我.
Correct me if i am wrong in my scenario.
谢谢
这篇关于可以访问所有节点的变形/考兰德验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!