Plone Content Types With Archetypes - Validators (Page 4 of 4 )
A problem with our Quotation product is that people can enter anything they want when creating a new object. For example, I could set the speaker of the quotation to “Benj4min Frankl1n”, which is unrealistic. Because of this, it might be a good idea to restrict what the user can enter as the speaker.
We can restrict what a use can enter by using things called validators. When a user submits data, any validators attached to that field are run. They look at the information submitted and make sure that everything is valid. If it is, the data is accepted. If it's not valid, then an error is returned.
There is more than one way to create a validator, but, in my opinion, the easiest is to create one out of the RegexValidator class. It accepts a regular expression, and it matches the value of any associated fields with the regular expression. In our example, we want to make sure that there are no numbers in the speaker's name. Add this code to your __init__.py file:
def initialize(context):
from Products.validation.validators.RegexValidator import
RegexValidator
from Products.validation import validation
validation.register(RegexValidator('isValidSpeaker',
r'\A\D*\Z', errmsg = ' contains irregular characters.'))
...
This registers our validator under the name of “isValidSpeaker” so that we can attach it to any field we'd like. Note that the registration of our validator must come before everything else in initialize. Modify the schema variable in Quotation.py and assign our validator to the field containing the speaker's name:
schema = BaseSchema + Schema((
TextField('speaker', required = True, validators =
['isValidSpeaker']),
TextField('quotation', required = True, widget =
TextAreaWidget)
))
Restart Zope and reinstall the product. Try to create a quotation with numbers in the speaker's name and examine what happens.
Conclusion
What's covered in this article is not, of course, all there is to Archetypes. Archetypes contains a lot more features—fields, widgets, built-in validators and many more interesting things. However, from what's been covered, I think it's pretty safe to conclude quite a few things about Archetypes.
Archetypes is a utility that allows developers to create Plone products very easily. A schema is created and put into a class. The schema contains fields which content types are built around. A __init__.py file is then created, along with an installation script. Archetypes then does its magic, generating interfaces for modifying and viewing objects. Using this process, a developer can create a simple content type with just a few lines of Python code.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |