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

Christian Zagrodnick cz at gocept.com
Thu Dec 14 07:54:53 EST 2006


Log message for revision 71548:
  Fixed widget bug in zope.app.form.browser; _getCurrentValue always
          returns an input value now. This fixes a bug in _getFormValue.
  

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

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2006-12-14 11:26:47 UTC (rev 71547)
+++ Zope3/trunk/doc/CHANGES.txt	2006-12-14 12:54:52 UTC (rev 71548)
@@ -149,6 +149,9 @@
 
     Bug fixes
 
+      - Fixed widget bug in zope.app.form.browser; _getCurrentValue always
+        returns an input value now. This fixes a bug in _getFormValue.
+
       - Fixed bug #717: formlib raised FormError when schema fields were
         missing from a request although not required.
 

Modified: Zope3/trunk/src/zope/app/form/browser/widget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/widget.py	2006-12-14 11:26:47 UTC (rev 71547)
+++ Zope3/trunk/src/zope/app/form/browser/widget.py	2006-12-14 12:54:52 UTC (rev 71548)
@@ -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,37 +370,55 @@
         else:
             return value
 
-    def _getCurrentValue(self):
-        """Detects the status of the widget and selectes either 
-        the input value that came from the request, the value from the 
-        _data attribute or the default value.
+    def _getCurrentValueHelper(self):
+        """Helper to get the current input value.
+        
+        Raises InputErrors if the data could not be validated/converted.
         """
-        if not self._renderedValueSet():
+        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()
-        else:
-            value = self._data
-        return value
+                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."""
-        value = self._getCurrentValue()
-        return self._toFormValue(value)
+        """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:
+            form_value = self._toFormValue(input_value)
+        return form_value
+
     def _getDefault(self):
         """Returns the default value for this widget."""
         return self.context.default



More information about the Zope3-Checkins mailing list