How do you define a custom validator in Deform/Colander that has access to all node values. I need to access the values from two fields in order to decide if a particular value is valid or not?
Asked
Active
Viewed 2,429 times
2 Answers
3
To place a validator for all colander fields we can simply do this
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):
#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.
Thanks

CrazyGeek
- 3,397
- 2
- 24
- 38
2
Tangibly the answer is:
def verify_email_validator(form, values):
if values['email_address'] != values['verify_email']:
raise Invalid(form, 'Email values do not match')
class MySchema(MappingSchema):
def __init__(self, *args, **kwargs):
super(KickEntrySchema, self).__init__(*args, **kwargs)
self.validator=verify_email_validator # entire form validator
email_address = SchemaNode(Email())
verify_email = SchemaNode(Email())
Note the form validator is invoked only if none of the individual field validators raise an error.

Doug
- 32,844
- 38
- 166
- 222
-
This solution doesn't work when you bind MySchema to a form in a view i.e. deform.Form(MySchema().bind(), buttons=('Submit', 'Cancel'), ....). Tim Hoffman's solution works and worth clicking as it proposes just passing a keyword parameter instead of overriding a method. – Alex Volkov Jul 10 '15 at 20:33