I faced the same situation and this how i achieve to raise the error as normal validation error.
Validator method:
def user_DoesExist(node,appstruct):
if DBSession.query(User).filter_by(username=appstruct['username']).count() > 0:
raise colander.Invalid(node, 'Username already exist.!!')
Schema:
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:
@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()}
Hope this will help you. Thanks.