Question

Deform allows to add validation on different fields of a form. However, it checks that the form is valid in itself but that does not necessarily mean that the form processing will be valid.

For example, if the form is for creating a new user with an email address. The form is valid but the form processing (which consists in inserting this new user in a database) rises up a database integrity error saying there is already a user with this email address.

I know I could add a special validator that checks the email is not already used but still, there could be another concurrent transaction with the same email that commits between the check and the commit of the first transaction, which is not 100% safe at the end.

So, how can I nicely report form post-processing errors to the user?

I could easily report the error messages next to the form (flash message or other) but I would like to know if there is a way to report the error directly in the widgets exactly like normal validation errors are handled.

No correct solution

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top