[Checkins] SVN: plone.z3cform/trunk/ Allow ICrudForm.add to raise
ValidationError, which allows for
Daniel Nouri
daniel.nouri at gmail.com
Fri Jul 25 04:42:58 EDT 2008
Log message for revision 88809:
Allow ICrudForm.add to raise ValidationError, which allows for
displaying a user-friendly error message.
Changed:
U plone.z3cform/trunk/docs/HISTORY.txt
U plone.z3cform/trunk/plone/z3cform/crud/README.txt
U plone.z3cform/trunk/plone/z3cform/crud/crud.py
-=-
Modified: plone.z3cform/trunk/docs/HISTORY.txt
===================================================================
--- plone.z3cform/trunk/docs/HISTORY.txt 2008-07-25 08:12:06 UTC (rev 88808)
+++ plone.z3cform/trunk/docs/HISTORY.txt 2008-07-25 08:42:57 UTC (rev 88809)
@@ -4,6 +4,9 @@
0.4 - Unreleased
----------------
+* Allow ICrudForm.add to raise ValidationError, which allows for
+ displaying a user-friendly error message.
+
* Make the default layout template CMFDefault- compatible.
0.3 - 2008-07-24
Modified: plone.z3cform/trunk/plone/z3cform/crud/README.txt
===================================================================
--- plone.z3cform/trunk/plone/z3cform/crud/README.txt 2008-07-25 08:12:06 UTC (rev 88808)
+++ plone.z3cform/trunk/plone/z3cform/crud/README.txt 2008-07-25 08:42:57 UTC (rev 88809)
@@ -213,6 +213,40 @@
>>> log
[]
+What if we try to add "Daniel" twice? Our current implementation of
+the add form will simply overwrite the data:
+
+ >>> save_daniel = storage['Daniel']
+ >>> html = MyForm(None, request)()
+ >>> "Item added successfully" in html
+ True
+ >>> save_daniel is storage['Daniel']
+ False
+ >>> log.pop().object is storage['Daniel']
+ True
+
+Let's implement a class that prevents this:
+
+ >>> class MyCarefulForm(MyForm):
+ ... def add(self, data):
+ ... name = data['name']
+ ... if name not in storage:
+ ... return super(MyCarefulForm, self).add(data)
+ ... else:
+ ... raise schema.ValidationError(
+ ... u"There's already an item with the name '%s'" % name)
+
+ >>> save_daniel = storage['Daniel']
+ >>> html = MyCarefulForm(None, request)()
+ >>> "Item added successfully" in html
+ False
+ >>> "There's already an item with the name 'Daniel'" in html
+ True
+ >>> save_daniel is storage['Daniel']
+ True
+ >>> len(log) == 0
+ True
+
Render some of the fields in view mode
--------------------------------------
Modified: plone.z3cform/trunk/plone/z3cform/crud/crud.py
===================================================================
--- plone.z3cform/trunk/plone/z3cform/crud/crud.py 2008-07-25 08:12:06 UTC (rev 88808)
+++ plone.z3cform/trunk/plone/z3cform/crud/crud.py 2008-07-25 08:42:57 UTC (rev 88809)
@@ -43,6 +43,9 @@
The `data` mapping corresponds to the schema returned by
`add_schema`.
+
+ May raise zope.schema.ValidationError to indicate that there's
+ a problem with the add form data.
"""
def remove((id, item)):
@@ -281,9 +284,13 @@
if errors:
self.status = form.AddForm.formErrorsMessage
return
- item = self.context.add(data)
- zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(item))
- self.status = _(u"Item added successfully.")
+ try:
+ item = self.context.add(data)
+ except zope.schema.ValidationError, e:
+ self.status = e
+ else:
+ zope.event.notify(zope.lifecycleevent.ObjectCreatedEvent(item))
+ self.status = _(u"Item added successfully.")
class NullForm(object):
def __init__(self, context, request):
More information about the Checkins
mailing list