Creating an Archetypes validator
Sun, Aug 27, 2006,
200 Words
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 validationNext, the source code for the validator (validators.py):
from validators import SamePasswordValidator
validation.register(SamePasswordValidator('isSamePassword'))
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."""