我遇到了同样的情况,这就是我如何将错误提升为正常验证错误。
验证器方法:
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):
schema = UserSchema(validator = user_DoesExist).bind(request=self.request)
form = deform.Form(schema, action=self.request.route_url('add_user'), buttons=('Add User','Cancel'))
if 'Cancel' in self.request.params:
return HTTPFound(location = self.request.route_url('home'))
if 'Add_User' in self.request.params:
appstruct = None
try:
appstruct = form.validate(self.request.POST.items())
except deform.ValidationFailure, e:
log.exception('in form validated')
return {'form':e.render()}
希望这会帮助你。谢谢。