4.3. Validator Reference

A quick reference to the built-in Archetypes validators.

Using Validators

Archetypes fields may have validators specified in the Field schema. For example, the schema for the basic page type includes the stanza:

ATDocumentSchema = ATContentTypeSchema.copy() + Schema((
    TextField('text',
...
              validators = ('isTidyHtmlWithCleanup',),
...
    ),

This specifies that the isTidyHtmlWithCleanup test will be applied to validate form input.

You may specify a sequence of validators:

validators = ('isMaxSize', 'isTidyHtmlWithCleanup',),

and the validators will tested in order.

The validators sequence may contain two kinds of entries:

  • The string names of validators registered with the validation service (see Products.validation);
  • Instances of classes implementing the IValidator interface (Products.validation.interfaces.IValidator.IValidator).

A validation specification using a validator class instance can look like:

validators = ( ExpressionValidator('python: int(value) == 5'), )

 

Registered Validators

These are validators pre-registered with the validation service. They may be specified by name.

NameUseDetails
isDecimalIs the input a decimal number.Allows exponent notation.
isIntIs the input an integer.
isPrintableDoes not contain unprintable charactersr'[a-zA-Z0-9\s]+$'
isSSNIs a well-formed social-security numberVery naive: r'^\d{9}$'
isUSPhoneNumberIs a valid US phone numberLooks for 10 digits, ignores spaces, parens and dashes
isInternationalPhoneNumberIs a valid international phone numberLooks for any number of digits, ignores spaces, parens and dashes
isZipCodeVery naive: is five or nine digits
isURLIs a valid URLRecognizes most protocols
isEmailIs a valid e-mail addressA pretty good regular expression test
isMailToIs an e-mail address preceded by "mailto:"
isUnixLikeNamePasses the basic test to be a Unix-style namer"^[A-Za-z][\w\d\-\_]{0,7}$"
isMaxSizeTests if an upload, file or something supporting len() is smaller than a given max size value.Tests against a maxsize attribute on the field
isValidDateTests whether or not input value can be converted to a DateTime object. 
isEmptyInput value must be empty. 
isEmptyNoErrorInput value must be empty.Validation will fail if input value is not empty; but no error will show.
isValidIdInput value is a valid identifier. 
isTidyHtmlUses mx.Tidy to validate HTML input. Fails on errors and warnings. 
isTidyHtmlWithCleanupUses mx.Tidy to validate HTML input. Fails only on errors; cleans up. 
isNonEmptyFileThe uploaded file is not empty. 
isTALValidates as Template Attribute Language 

 

Useful Validation Classes

These classes are useful for creating your own validation class instances. Imports and prototypes are shown. See source for details.

ExpressionValidator

Evaluates an expression to test the input value.

from Products.validation.validators.ExpressionValidator import ExpressionValidator

def ExpressionValidator(expression=None, errormsg=None)

RegexValidator

Tests value against a regular expression after removing ignore

characters.

from Products.validation.validators.RegexValidator import RegexValidator

def RegexValidator(name, regex, title=name, description='',
 errmsg='fails tests of %s' % name, ignore=None)

RangeValidator

Tests to see if specified minval <= input_value < maxval

from Products.validation.validators.RangeValidator import RangeValidator

def RangeValidator(name, minval=0.0, maxval=0.0, title='', description='')

ExpressionValidator for True/False

Posted by Kees Hink (Goldmund, Wyldebeast & Wunderliebe) at Apr 17, 2008 12:12 PM
When validating a boolean for truth (for the case of a site where members have to accept terms of use before they can join), I was fooled at first by the way the form feedback is handled. I succeeded by adding this field to the member content type (a remember sterotype):

    atapi.BooleanField(
        name='acceptTerms',
        required=1,
        regfield=1,
        validators = ( ExpressionValidator('python: (value == 1) or (value ==\'1\') or (value == True)', 'You must accept the terms in order to become a member.'), ),
        widget=atapi.BooleanWidget(
            label="Accept terms of use",
            description="By clicking this box, you acknowledge that you have read and understood the <a href=\"terms\" target=\"top\">terms of use</a>, and will use your account in compliance with these terms.",

Probably, just (value=='1') should do it, as the browser sends a '1' string for True, and '0' string for False.