[Zope-CMF] Validation during object creation

Geoff Davis geoff@geoffdavis.net
Mon, 8 Jul 2002 11:45:42 -0400


I'm trying to put together a basic mechanism for doing form validation
during object creation.  Specifically, I would like to be able to bypass the
standard "Add Content" form via the following kind of mechanism:

I have a class of objects called MyObject that I want to manage.  To create
such a MyObject, one invokes a python method called addMyObject.
addMyObject initially sets some values and passes them to a ZPT called
addMyObjectForm.  The form submits to addMyObject.  addMyObject then
validates the parameters -- if they are invalid, it passes the user's
responses back to addMyObjectForm; if they are valid, it makes the
appropriate call to invokeFactory and then relocates to some other page.

A few questions:

1) Before I reinvent a wheel, is there already an easy way to do this in
Zope?  I haven't seen anything yet

2) I am writing a helper object that takes care of most of the validation
process above.  At construction you designate a validation string (sort of
like http://www.zope.org/Members/k_vertigo/Products/PageContract), a
PageTemplate to display when prompting for parameters needed to build your
object, a method used to construct the object once you have a valid set of
parameters, and a URL to go to once you have finished construction.  In the
end it should look something like this:

class MyObject:
   def modify(some args here):
      some code here

   modifyMyObject = ValidatedForm("id: required; foo: integer,
required('Please enter an integer'); bar: string, trim, optional")

addMyObject = ValidatedForm("id: required; foo: integer, required('Enter an
integer, dammit'); bar: string, trim, optional")

I have been trying to add addMyObject as an external method, but have been
encountering weird errors that I think are arising from
ValidatedForm.__call__ not having the right function signature.  Given that
ExternalMethod seems to expect a method with the signature foo(self), I'm
not sure how to write an appropriate __call__ signature (__call__(self,
self)??).  Any ideas?  The specific error I'm getting is on line 143 of
ExternalMethod.py (in Zope 2.5.1): ff doesn't have an attribute
func_defaults.

I am thinking the right way to do this is to not use external methods and
rather to build a more generic factory object that uses these validated
forms and then delegates to portal_types.  Given that this is looking more
involved, question 1 above becomes even more relevant.

Geoff Davis
http://www.geoffdavis.net