Report error message after processing in HTML controls using Deform

Deform allows you to add confirmation to different form fields. However, he verifies that the form is valid in itself, but this does not necessarily mean that the processing of the form will be valid.

For example, if the form is intended to create a new user with an email address. The form is valid, but processing the form (which consists of inserting this new user into the database) increases the integrity error of the database, since there is already a user with this email address.

I know that I can add a special validator that checks that the letter is not yet in use, but still there may be another parallel transaction with the same email address that commits the transaction between checking and fixing the first transaction, which is not 100% safe in the end.

So, how can I clearly inform the user about errors after processing?

I can easily report errors next to the form (flash message or something else), but I would like to know if there is a way to report an error directly in the widgets in the same way that regular validation errors are handled.

+4
source share
1 answer

I faced the same situation, and this is how I achieve to raise the error as a regular verification error.

Verification Method:

def user_DoesExist(node,appstruct): if DBSession.query(User).filter_by(username=appstruct['username']).count() > 0: raise colander.Invalid(node, 'Username already exist.!!') 

Scheme:

 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 helps you. Thanks.

+1
source

All Articles