[Zope-CMF] Re: Validation during object creation
Geoff Davis
geoff@geoffdavis.net
Mon, 8 Jul 2002 12:50:42 -0400
A few clarifying points that may help explain the reasons I'm going through
all this trouble:
* I want to ensure that the user specifies certain attribute properties
before I create an object. I want to require more than an ID and a Title.
I don't want partially-populated objects lying around that I will have to
clean up later.
* I want to be able to prompt for the initial attributes directly, using my
own ZPT, and without going through the CMF's ID / Title prompt. I want to
be able to prepopulate some fields programmatically.
* When a user does not fill out a form correctly, I want to display both
error messages and the user's original entries on the same form. *** I
don't want the user to get kicked to a different page with error messages
and then have to hit the back button to make corrections. ***
* Ideally I would like to be able to have most validation done in my python
code rather than something I generate using the through-the-web interface.
Ideally I would like something like PageContract to be able to handle most
things, with a simple way to augment these more automated validations.
Geoff Davis
http://www.geoffdavis.net
----- Original Message -----
From: "Geoff Davis" <geoff@geoffdavis.net>
To: "Zope CMF" <zope-cmf@zope.org>
Sent: Monday, July 08, 2002 11:45 AM
Subject: Validation during object creation
> 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
>