disabling zope.schema constraint check in edit form
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. But in edit form if I modify any other field other than the 'name' field I get the 'NameAlreadyExists' error. 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? What is the preferred way of doing these kind of checks? Please guide me. -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
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-... Mit freundlichen Grüßen -- Michael Howitz · mh@gocept.com · 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
Hello Michael, Thanks for the reply. On Thu, 2011-08-18 at 12:46 +0200, Michael Howitz wrote:
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.
Yes. But considering the fact that I am doing this check at the interface level. I wonder if that is ever possible, because the constraint method knows just the value of the field.
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-...
I'll check this and get back. Regards Joshua -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
Am 18.08.2011, 13:19 Uhr, schrieb Joshua Immanuel <josh@hipro.co.in>:
Yes. But considering the fact that I am doing this check at the interface level. I wonder if that is ever possible, because the constraint method knows just the value of the field.
You can always go from the field to the object to which it is bound. Not sure if your constraint is actually the best way to go. Charlie -- Charlie Clark Managing Director Clark Consulting & Research German Office Helmholtzstr. 20 Düsseldorf D- 40215 Tel: +49-211-600-3657 Mobile: +49-178-782-6226
Hello Michael, On Thu, 2011-08-18 at 12:46 +0200, Michael Howitz wrote:
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-...
Thanks for your excellent article. Google translate helped me to understand your German text :) Instead of going via the constraint approach I will try using your approach. I see many people using z3c.form it makes me wonder whether to follow their trail or to stick with zope.formlib. -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
Hi Joshua, On Thu, Aug 18, 2011 at 09:59, Joshua Immanuel <josh@hipro.co.in> wrote:
[...] Worse case scenario is where I have a cancel action button which just redirects to another page, that too screams for the NameAlreadyExists error.
For the 'cancel button' case, you need to have a form action with a validator that always validates, no matter what. You can find an example of one such null_validator here: https://svn.plone.org/svn/plone/plone.app.form/tags/2.0.3/plone/app/form/val... To use it, you do something like class MyForm(...): @form.action(..., validator=null_validator): def handle_cancel(self, ...) [... do the redirect ...] Cheers, Leo
[...]
Hello Leonardo, On Thu, 2011-08-18 at 19:10 +0200, Leonardo Rochael Almeida wrote:
For the 'cancel button' case, you need to have a form action with a validator that always validates, no matter what. You can find an example of one such null_validator here:
https://svn.plone.org/svn/plone/plone.app.form/tags/2.0.3/plone/app/form/val...
To use it, you do something like
class MyForm(...):
@form.action(..., validator=null_validator): def handle_cancel(self, ...) [... do the redirect ...]
Thanks for the useful tip. I'll use this :) Regards Joshua -- Joshua Immanuel HiPro IT Solutions Private Limited http://hipro.co.in
participants (4)
-
Charlie Clark -
Joshua Immanuel -
Leonardo Rochael Almeida -
Michael Howitz