Personal tools
You are here: Home Documentation Manuals Archetypes Developer Manual Fields Validator Reference
Support

Get Help

Join our chat rooms or support forums if you have more specific questions.

Plone Training
Learn how to design, build, and deploy a website in Plone through one of the numerous Plone training sessions around the world.
Find Plone training…
 
Document Actions

3.4. Validator Reference

Up one level
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.

Name Use Details
isDecimal Is the input a decimal number. Allows exponent notation.
isInt Is the input an integer.
isPrintable Does not contain unprintable characters r'[a-zA-Z0-9\s]+$'
isSSN Is a well-formed social-security number Very naive: r'^\d{9}$'
isUSPhoneNumber Is a valid US phone number Looks for 10 digits, ignores spaces, parens and dashes
isInternationalPhoneNumber Is a valid international phone number Looks for any number of digits, ignores spaces, parens and dashes
isZipCode Very naive: is five or nine digits
isURL Is a valid URL Recognizes most protocols
isEmail Is a valid e-mail address A pretty good regular expression test
isMailTo Is an e-mail address preceded by "mailto:"
isUnixLikeName Passes the basic test to be a Unix-style name r"^[A-Za-z][\w\d\-\_]{0,7}$"
isMaxSize Tests if an upload, file or something supporting len() is smaller than a given max size value. Tests against a maxsize attribute on the field
isValidDate Tests whether or not input value can be converted to a DateTime object.  
isEmpty Input value must be empty.  
isEmptyNoError Input value must be empty. Validation will fail if input value is not empty; but no error will show.
isValidId Input value is a valid identifier.  
isTidyHtml Uses mx.Tidy to validate HTML input. Fails on errors and warnings.  
isTidyHtmlWithCleanup Uses mx.Tidy to validate HTML input. Fails only on errors; cleans up.  
isNonEmptyFile The uploaded file is not empty.  
isTAL Validates 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='')
by Steve McMahon last modified January 18, 2008 - 22:05
Contributors: Sisi Nutt, Alexander Limi, Martin Aspeli, Marco De Vitis, Raphael Ritz, Maik Röder, Steve McMahon
All content is copyright Plone Foundation and the individual contributors.

ExpressionValidator for True/False

Posted by Kees Hink at April 17, 2008 - 12:12
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.

For any issues with the web site functionality, please file a ticket.

Please consult the policy on plone.org content if you want your content published on this site.

Servers and hosting by