Re: disabling zope.schema constraint check in edit form

Michael Howitz <[email protected]>
Newsgroups gmane.comp.web.zope.bluebream,gmane.comp.web.zope.devel
Message-ID <[email protected]>
Am 18.08.2011 um 09:59 schrieb Joshua Immanuel:
> Hello all,
> 
> I use the zope.schema field's constraint parameter to check for the
> uniqueness of a particular field like this
> 
>        class IMyObject(Interface):
> 
>                def check_for_uniqueness(value):
>                        cat = getUtility(ICatalog)
>                        results = cat.searchResults(object_name=value)
>                        for rslt in results:
>                                if rslt.name.lower() == value.lower():
>                                        raise NameAlreadyExists(value)
>                        return True
> 
>                name = TextLine(title=u'Name', required=True,
>                		constraint=check_for_uniqueness)
>                ...
> 
> This goes well with the zope.formlib's AddForm.

As the value does not exist yet.

> But in edit form if I
> modify any other field other than the 'name' field I get the
> 'NameAlreadyExists' error.

As you find the object which you are editing in the catalog.

> Worse case scenario is where I have a cancel
> action button which just redirects to another page, that too screams for
> the NameAlreadyExists error.
> 
> Is there a way to disable the constraint check in the EditForm if the
> 'name' field isn't modified?

Make sure your catalog search does not return the object you are currently editing.

> What is the preferred way of doing these kind of checks?

Some time a ago I wrote a blog post about objects with attributes which are unique in their container [1].
Besides it is written in German you might get the clue from the code examples.


[1] http://blog.gocept.com/zope3-objekte-mit-eindeutigen-titeln-innerhalb-eines-ordners


Mit freundlichen Grüßen
-- 
Michael Howitz · [email protected] · Softwareentwickler
gocept gmbh & co. kg · Forsterstraße 29 · 06112 Halle (Saale) · Deutschland
http://gocept.com · Tel +49 345 1229889 8 · Fax +49 345 1229889 1
Zope- und Plone-Beratung und -Entwicklung
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.