[Zope-CMF] Validation during object creation

kapil thangavelu kthangavelu@earthlink.net
Sat, 13 Jul 2002 14:39:02 -0700


hi geoff,

one thing to note regarding validation with page contract, its useable from 
within python scripts and products. so an example python script addMyObject 
could be...

from Products.PageContract.Scripts import compile

req = context.REQUEST

contract = """
# parameters
id: integer
bar: optional, integer
# errors
default: myForm
"""
contract = compile(contract)
contract.validate(req)
vars = contract.extract_variables(req)

container.manage_addProduct['myProduct'].manage_AddFoo(
    id = vars.id,
    bar =  vars.bar
)

the original form is designated as the error handler.

another option, if you don't need pipelined input validation/transformation 
is to use something like the form_tool thats in plone.

hth,

k

On Monday 08 July 2002 08:45 am, Geoff Davis wrote:
> 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
>
>
>
> _______________________________________________
> Zope-CMF maillist  -  Zope-CMF@zope.org
> http://lists.zope.org/mailman/listinfo/zope-cmf
>
> See http://www.zope.org/Products/PTK/Tracker for bug reports and feature
> requests