[Zope3-checkins] SVN: Zope3/branches/3.3/ - Fixed widget bug in zope.app.form.browser; _getCurrentValue always returns

Christian Theune ct at gocept.com
Wed Dec 20 02:27:23 EST 2006


Log message for revision 71611:
   - Fixed widget bug in zope.app.form.browser; _getCurrentValue always returns
     an input value now. This fixes a bug in _getFormValue.
  
     (This required a backport of a small restructuring: Changed internal widget
     API to allow retrieving the current value (from request, default or current
     field value) in addition to the current form representation of the value.)
  

Changed:
  U   Zope3/branches/3.3/doc/CHANGES.txt
  U   Zope3/branches/3.3/src/zope/app/form/browser/widget.py

-=-
Modified: Zope3/branches/3.3/doc/CHANGES.txt
===================================================================
--- Zope3/branches/3.3/doc/CHANGES.txt	2006-12-19 20:50:29 UTC (rev 71610)
+++ Zope3/branches/3.3/doc/CHANGES.txt	2006-12-20 07:27:21 UTC (rev 71611)
@@ -10,6 +10,14 @@
 
     Bugfixes
 
+      - Fixed widget bug in zope.app.form.browser; _getCurrentValue always
+        returns an input value now. This fixes a bug in _getFormValue.
+
+        (This required a backport of a small restructuring: Changed internal
+        widget API to allow retrieving the current value (from request,
+        default or current field value) in addition to the current form
+        representation of the value.)
+
       - Fixed bug #707: "layer" directive was marked as deprecated in a
         confusing way.
 

Modified: Zope3/branches/3.3/src/zope/app/form/browser/widget.py
===================================================================
--- Zope3/branches/3.3/src/zope/app/form/browser/widget.py	2006-12-19 20:50:29 UTC (rev 71610)
+++ Zope3/branches/3.3/src/zope/app/form/browser/widget.py	2006-12-20 07:27:21 UTC (rev 71611)
@@ -253,11 +253,17 @@
         ...             return float(input)
         ...         except ValueError, v:
         ...             raise ConversionError('Invalid floating point data', v)
+        ...
+        ...     def _toFormValue(self, value):
+        ...         value = super(FloatWidget, self)._toFormValue(value)
+        ...         return '%.2f' % value
 
         >>> request = TestRequest(form={'field.price': u'32.0'})
         >>> widget = FloatWidget(field, request)
         >>> widget.getInputValue()
         32.0
+        >>> widget()
+        u'<input class="textType" id="field.price" name="field.price" type="text" value="32.00"  />'
 
         >>> request = TestRequest(form={'field.price': u'foo'})
         >>> widget = FloatWidget(field, request)
@@ -266,7 +272,10 @@
         ... except ConversionError, error:
         ...     print error.doc()
         Invalid floating point data
+        >>> widget()
+        u'<input class="textType" id="field.price" name="field.price" type="text" value="foo"  />'
 
+
     >>> tearDown()
     """
 
@@ -361,28 +370,54 @@
         else:
             return value
 
-    def _getFormValue(self):
-        """Returns a value suitable for use in an HTML form."""
-        if not self._renderedValueSet():
+    def _getCurrentValueHelper(self):
+        """Helper to get the current input value.
+        
+        Raises InputErrors if the data could not be validated/converted.
+        """
+        input_value = None
+        if self._renderedValueSet():
+            input_value = self._data
+        else:
             if self.hasInput():
-
                 # It's insane to use getInputValue this way. It can
                 # cause _error to get set spuriously.  We'll work
                 # around this by saving and restoring _error if
                 # necessary.
                 error = self._error
                 try:
-                    try:
-                        value = self.getInputValue()
-                    except InputErrors:
-                        return self.request.form.get(self.name, self._missing)
+                    input_value = self.getInputValue()
                 finally:
                     self._error = error
             else:
-                value = self._getDefault()
+                input_value = self._getDefault()
+        return input_value
+
+    def _getCurrentValue(self):
+        """Returns the current input value.
+
+        Returns None if the data could not be validated/converted.
+        """
+        try:
+            input_value = self._getCurrentValueHelper()
+        except InputErrors:
+            input_value = None
+        return input_value
+
+    def _getFormValue(self):
+        """Returns a value suitable for use in an HTML form.
+
+        Detects the status of the widget and selects either the input value
+        that came from the request, the value from the _data attribute or the
+        default value.
+        """
+        try:
+            input_value = self._getCurrentValueHelper()
+        except InputErrors:
+            form_value = self.request.form.get(self.name, self._missing)
         else:
-            value = self._data
-        return self._toFormValue(value)
+            form_value = self._toFormValue(input_value)
+        return form_value
 
     def _getDefault(self):
         """Returns the default value for this widget."""



More information about the Zope3-Checkins mailing list