Zope
  Home arrow Zope arrow Page 4 - Plone Content Types With Archetypes
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
ZOPE

Plone Content Types With Archetypes
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2006-02-28


    Table of Contents:
  • Plone Content Types With Archetypes
  • A Quotation Content Type
  • Mutators and Accessors
  • Validators

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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.



     
     
    >>> More Zope Articles          >>> More By Peyton McCullough
     

       

    ZOPE ARTICLES

    - Creating Zope Products
    - Plone Content Types With Archetypes
    - Flat User Management in Zope
    - Creating Basic Zope Applications
    - Getting started with Zope for Linux and Sola...
    - ZPT Basics (part 4)
    - ZPT Basics (part 3)
    - ZPT Basics (part 2)
    - ZPT Basics (part 1)
    - Exception Handling In DTML
    - DTML Basics (part 4)
    - DTML Basics (part 3)
    - DTML Basics (part 2)
    - DTML Basics (part 1)
    - Using Zope With Apache





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek