3.4.
Validator Reference
Up one level
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 ignorecharacters.
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
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.