[Checkins] SVN: z3c.form/trunk/ Feature: The widget manager's ``extract()`` method now supports an optional

Stephan Richter srichter at cosmos.phy.tufts.edu
Wed Jul 9 11:19:59 EDT 2008


Log message for revision 88143:
  Feature: The widget manager's ``extract()`` method now supports an optional
  ``setErrors`` (default value: True) flag that allows one to not set errors
  on the widgets and widget manager during data extraction. Use case: You want
  to inspect the entered data and handle errors manually.
  
  

Changed:
  U   z3c.form/trunk/CHANGES.txt
  U   z3c.form/trunk/src/z3c/form/field.py
  U   z3c.form/trunk/src/z3c/form/field.txt

-=-
Modified: z3c.form/trunk/CHANGES.txt
===================================================================
--- z3c.form/trunk/CHANGES.txt	2008-07-09 15:08:14 UTC (rev 88142)
+++ z3c.form/trunk/CHANGES.txt	2008-07-09 15:19:58 UTC (rev 88143)
@@ -2,9 +2,14 @@
 CHANGES
 =======
 
-Version 1.8.3 (????-??-??)
+Version 1.9.0 (????-??-??)
 --------------------------
 
+- Feature: The widget manager's ``extract()`` method now supports an optional
+  ``setErrors`` (default value: True) flag that allows one to not set errors
+  on the widgets and widget manager during data extraction. Use case: You want
+  to inspect the entered data and handle errors manually.
+
 - Bug: Added a widget for ``IDecimal`` field in testing setup.
 
 - Feature: The ``z3c.form.util`` module has a new function, ``createCSSId()``

Modified: z3c.form/trunk/src/z3c/form/field.py
===================================================================
--- z3c.form/trunk/src/z3c/form/field.py	2008-07-09 15:08:14 UTC (rev 88142)
+++ z3c.form/trunk/src/z3c/form/field.py	2008-07-09 15:19:58 UTC (rev 88143)
@@ -264,10 +264,10 @@
             self._data[shortName] = widget
             zope.location.locate(widget, self, shortName)
 
-    def extract(self):
+    def extract(self, setErrors=True):
         """See interfaces.IWidgets"""
         data = {}
-        self.errors = ()
+        errors = ()
         for name, widget in self.items():
             if widget.mode == interfaces.DISPLAY_MODE:
                 continue
@@ -288,8 +288,9 @@
                     (error, self.request, widget, widget.field,
                      self.form, self.content), interfaces.IErrorViewSnippet)
                 view.update()
-                widget.error = view
-                self.errors += (view,)
+                if setErrors:
+                    widget.error = view
+                errors += (view,)
             else:
                 name = widget.__name__
                 data[name] = value
@@ -298,5 +299,7 @@
                 (error, self.request, None, None, self.form, self.content),
                 interfaces.IErrorViewSnippet)
             view.update()
-            self.errors += (view,)
-        return data, self.errors
+            errors += (view,)
+        if setErrors:
+            self.errors = errors
+        return data, errors

Modified: z3c.form/trunk/src/z3c/form/field.txt
===================================================================
--- z3c.form/trunk/src/z3c/form/field.txt	2008-07-09 15:08:14 UTC (rev 88142)
+++ z3c.form/trunk/src/z3c/form/field.txt	2008-07-09 15:19:58 UTC (rev 88143)
@@ -682,7 +682,35 @@
 well, just as it is the case for field-specific validation errors. And that's
 really all there is!
 
+By default, the ``extract()`` method not only returns the errors that it
+catches, but also sets them on individual widgets and on the manager:
 
+  >>> manager.errors
+  (<ErrorViewSnippet for LastNameTooShort>,)
+
+This behavior can be turned off. To demonstrate, let's make a new request that
+causes a widget-level error:
+
+  >>> request = TestRequest(form={
+  ...     'form.widgets.firstName': u'Stephan', 'form.widgets.id': u'srichter'})
+  >>> manager = field.FieldWidgets(personForm, request, context)
+  >>> manager.ignoreContext = True
+  >>> manager.update()
+
+When calling the ``extract()`` method with the `setErrors` flag turned off, we
+still get the same result from the method call, ...
+
+  >>> manager.extract(setErrors=False)
+  ({'firstName': u'Stephan'}, (<ErrorViewSnippet for RequiredMissing>,))
+
+but there are no side effects on the manager and the widgets:
+
+  >>> manager.errors
+  ()
+  >>> manager['lastName'].error is None
+  True
+
+
 Fields -- Custom Widget Factories
 ---------------------------------
 



More information about the Checkins mailing list