[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/ Fix error handling

Roger Ineichen roger at projekt01.ch
Sat Apr 22 21:38:24 EDT 2006


Log message for revision 67282:
  Fix error handling
  
  Widget have to raise WidgetInputError and not ValidationError
  directly. Improve sequence input widget base class methods
  getInputValue. This will ensure that sequence widgets will
  set the raised error to the _error attribute before the error 
  get raised.

Changed:
  U   Zope3/trunk/src/zope/app/form/browser/sequencewidget.py
  U   Zope3/trunk/src/zope/app/form/browser/tests/test_sequencewidget.py

-=-
Modified: Zope3/trunk/src/zope/app/form/browser/sequencewidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/sequencewidget.py	2006-04-23 01:33:36 UTC (rev 67281)
+++ Zope3/trunk/src/zope/app/form/browser/sequencewidget.py	2006-04-23 01:38:24 UTC (rev 67282)
@@ -19,6 +19,7 @@
 
 from zope.interface import implements
 from zope.i18n import translate
+from zope.schema.interfaces import ValidationError
 
 from zope.app import zapi
 from zope.app.form.interfaces import IDisplayWidget, IInputWidget
@@ -156,7 +157,16 @@
             self.preserve_widgets = True
             sequence = self._type(self._generateSequence())
             if sequence != self.context.missing_value:
-                self.context.validate(sequence)
+                # catch and set field errors to ``_error`` attribute
+                try:
+                    self.context.validate(sequence)
+                except WidgetInputError, error:
+                    self._error = error
+                    raise self._error
+                except ValidationError, error:
+                    self._error = WidgetInputError(
+                        self.context.__name__, self.label, error)
+                    raise self._error
             elif self.context.required:
                 raise MissingInputError(self.context.__name__,
                                         self.context.title)
@@ -209,7 +219,13 @@
         for i in reversed(range(count)):
             widget = self._getWidget(i)
             if widget.hasValidInput():
-                sequence[i] = widget.getInputValue()
+                # catch and set sequence widget errors to ``_error`` attribute
+                try:
+                    sequence[i] = widget.getInputValue()
+                except WidgetInputError, error:
+                    self._error = error
+                    raise self._error
+
             remove_key = "%s.remove_%d" % (self.name, i)
             if remove_key in self.request.form and removing:
                 del sequence[i]

Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_sequencewidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_sequencewidget.py	2006-04-23 01:33:36 UTC (rev 67281)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_sequencewidget.py	2006-04-23 01:38:24 UTC (rev 67282)
@@ -31,7 +31,7 @@
 from zope.app.form.browser import SequenceWidget
 from zope.app.form.interfaces import IDisplayWidget
 from zope.app.form.interfaces import IInputWidget, MissingInputError
-from zope.app.form.interfaces import IWidgetInputError
+from zope.app.form.interfaces import IWidgetInputError, WidgetInputError
 from zope.app.form.browser.interfaces import IWidgetInputErrorView
 from zope.app.form import CustomWidgetFactory
 from zope.app.form.browser.exception import WidgetInputErrorView
@@ -148,7 +148,7 @@
         widget = ListSequenceWidget(
             self.field, self.field.value_type, request)
         self.assert_(widget.hasInput())
-        self.assertRaises(ValidationError, widget.getInputValue)
+        self.assertRaises(WidgetInputError, widget.getInputValue)
 
         request = TestRequest(form={'field.foo.0.bar': u'Hello world!',
                                     'field.foo.count': u'1'})
@@ -172,7 +172,7 @@
         widget = TupleSequenceWidget(
             self.field, self.field.value_type, request)
         self.assert_(widget.hasInput())
-        self.assertRaises(ValidationError, widget.getInputValue)
+        self.assertRaises(WidgetInputError, widget.getInputValue)
         check_list = (
             'checkbox', 'field.foo.remove_0', 'input', 'field.foo.0.bar',
             'submit', 'submit', 'field.foo.add'



More information about the Zope3-Checkins mailing list