Creating an Archetypes validator

Archetypes validators are used in the schema definition for a field. Default validators include isEmail, isURL, etc.

This is how to create a new validator:

First, the validator has to be registered with Archetypes, or zope will complain at startup and ignore the validator. So add something like this in the __init__.py of the product:

from Products.validation import validation
from validators import SamePasswordValidator
validation.register(SamePasswordValidator('isSamePassword'))
Next, the source code for the validator (validators.py):
from Products.validation.interfaces import ivalidator

class SamePasswordValidator:
__implements__ = (ivalidator,)

def __init__(self, name):
self.name = name

def __call__(self, value, *args, **kwargs):
instance = kwargs.get('instance', None)
req = kwargs['REQUEST']
pass1 = req.form.get('password', None)
if pass1 <> value:
return """Validation failed: You need to enter the password twice."""

Comments