Creating Custom Validators
This How-to applies to: Any version.
Template Attribute Language Expression Syntax (TALES) is a simple notation that allows determination of a value via path (as in path/to/object), string or Python expressions. It is used in Zope's Template Attribute Language (TAL), and is ubiquitous in Plone templates. This how-to does not teach you TALES; for that, try the Zope Page Templates Reference.
Please note that it's easy to make a mistake when working with TALES fields that will cause an error when you try to display your form. Stay calm! Take note of the error message, and return to the field edit form to fix it. (Details in the FAQ) Don't be scared of this kind of error.
The rules for writing a validator are:
- You should validate against the the variable value, which will contain the field input. Note that -- for simple fields -- value will be a string. But, for a lines field, the contents of value will be a list.
- Return False or zero if you wish to accept the input.
- Return a string containing a user-feedback message if you don't wish to accept the input.
- Don't change the value variable. It won't do you any good.
A Simple Example
Let's say that you are operating a restaurant that serves only dishes containing spam. You may wish to check to make sure that the input to a string or text field contains "spam". You may do that with by setting a custom validator that reads:
python: test(value.find('spam')>=0, False, 'Input must include spam.')
This TALES/python expression uses the "test" function, which is available for use in TALES. It works somewhat like the C ternary operator:
test('test condition', 'return this if true', 'return this if false')
In our example, the test function looks to find the string 'spam' in value. If it finds it, it returns False; if it doesn't find it, it returns the terse error string 'Input must include spam.'
If you use test, watch out for a big difference from the C ternary operator: the evaluation of test does not short-circuit. All terms are evaluated, and must not raise exceptions.
The Name Space
Here are the objects available when your expression is evaluated.
| TALES Context | |
|---|---|
| value | The field input. |
| here | The current object. A bit dangerous since this varies depending on context. |
| folder | This will be your form folder. |
| portal | The portal object. |
| request | The REQUEST object. Note that request/form contains form input. |
| member | The authenticated user's member data -- if any. |
| nothing | Equivalent to Python None. |
| folder_url | URL of the form folder. |
| portal_url | URL of the site. |
| modules | Module importer. |
Using a Python Script
You'll be frustrated fast if you try to do anything smart in a single TALES expression. If you need to do something more complicated, add a Python Script to your form folder and call it via TALES. For example, if you added a script with the id includesSpam, you could call it with the expression:
python: folder.includesSpam(value)
Make sure your script returns False if you wish to accept the input, or an error string otherwise.
Here's what a validator script to check for spam might look like:
if value.lower().find('spam') >= 0:
return False
else:
return "'%s' doesn't seem to have spam. Try again." % value
Make sure your script parameter list includes value. (Alternatively, you may check the request.form dictionary,
which will include form input.)
Note: Python scripts are not the same as the Custom Script Adapter. The latter is meant to make it easy to add a custom adapter that's processed in the same way as the mail or save-data adapter. Python scripts are just simple Python code fragments that act like functions. They are added via the Zope Management Interface
where to find it