[Grok-dev] distinguish between a create or an edit situation in a formlib constraint

Steve Schmechel steveschmechel at yahoo.com
Fri Jul 31 00:30:09 EDT 2009


How are your views configured?  You could use the same schema in an "add" and an "edit" view.  Instead of placing the constraint on the schema, you could check for pre-existence only in the "add" view.

As an alternative, if you are using @grok.action to create buttons on your form, you can pass an overridden validator as one of the options.

Some References: 
http://grok.zope.org/documentation/tutorial/musical-performance-organizer-part-1/adding-musicians-to-a-performance

http://grok.zope.org/documentation/how-to/automatic-form-generation

http://grok.zope.org/doc/current/reference/decorators.html#grok-action-declare-a-form-submit-handler

http://pypi.python.org/pypi/zope.formlib/3.6.0#actions

Regards,
Steve

--- On Thu, 7/30/09, Dr. Volker Jaenisch <volker.jaenisch at inqbus.de> wrote:

> From: Dr. Volker Jaenisch <volker.jaenisch at inqbus.de>
> Subject: [Grok-dev] distinguish between a create or an edit situation in a formlib constraint
> To: grok-dev at zope.org
> Date: Thursday, July 30, 2009, 8:18 PM
> Hi Grok-People!
> 
> Maybe this should go better to the formlib people
> but Grok wrapps formlib in a special way ..
> 
> I have property "name" that should be unique
> 
> class IKvmInstance(interface.Interface):
>     """Defines a KVM instance."""
>     name = schema.TextLine(
>         title = u'KVM Name',
>         default = u'VM1',
>         required = True,
>         constraint = validateVMName,
>     )
> 
> In validateVMName I check for the existance of a VM with
> the same name 
> and reject
> the request if a VM with the given name already exists.
> 
> from zope.schema import ValidationError
> 
> class VMNameAlreadyInUse(ValidationError):
>         "VM Name already in use"
> 
> def validateVMName( name ):
>     # Why the heck this is called at grok
> boot-time??
>     if grok.getSite() == None : return True
> 
>     if
> grok.getSite().kvmManager.kvmContainer.has_key( name ):
>         raise VMNameAlreadyInUse
> 
>     return True
> 
> This work fine for the creation but fails on editing a
> KVMInstance. Is 
> there a generically way to do it?
> Or will I have to impose a flag on the instance that I
> check to see that 
> the instance is already created?
> 
> How can I seperate the creating and the edition use-case
> programmatically.
> 
> Any Hints appreciated
> 
> Best Reagards
> 
> Volker
> 
> 
> -- 
> ====================================================
>    inqbus it-consulting     
> +49 ( 341 )  5643800
>    Dr.  Volker Jaenisch   
>   http://www.inqbus.de
>    Herloßsohnstr.    12 
>     0 4 1 5 5    Leipzig
>    N  O  T -  F Ä L L
> E      +49 ( 170 )  3113748
> ====================================================
> 
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
> 


      


More information about the Grok-dev mailing list