[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/te Fixed a bug where IntWidget would not render a zero. The fix might be

Gintautas Miliauskas gintas at pov.lt
Mon Jan 24 09:35:02 EST 2005


Log message for revision 28919:
  Fixed a bug where IntWidget would not render a zero.  The fix might be
  pertinent to other widgets too.
  

Changed:
  U   Zope3/trunk/src/zope/app/form/browser/tests/test_intwidget.py
  U   Zope3/trunk/src/zope/app/form/browser/textwidgets.py

-=-
Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_intwidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_intwidget.py	2005-01-24 10:30:24 UTC (rev 28918)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_intwidget.py	2005-01-24 14:35:01 UTC (rev 28919)
@@ -28,7 +28,7 @@
 
 class IntWidgetTest(SimpleInputWidgetTest):
     """Documents and tests the int widget.
-        
+
         >>> verifyClass(IInputWidget, IntWidget)
         True
     """

Modified: Zope3/trunk/src/zope/app/form/browser/textwidgets.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2005-01-24 10:30:24 UTC (rev 28918)
+++ Zope3/trunk/src/zope/app/form/browser/textwidgets.py	2005-01-24 14:35:01 UTC (rev 28919)
@@ -121,29 +121,23 @@
         super(TextWidget, self).__init__(*args)
 
     def __call__(self):
-        displayMaxWidth = self.displayMaxWidth or 0
-        if displayMaxWidth > 0:
-            return renderElement(self.tag,
-                                 type=self.type,
-                                 name=self.name,
-                                 id=self.name,
-                                 value=self._getFormValue() or '',
-                                 cssClass=self.cssClass,
-                                 style=self.style,
-                                 size=self.displayWidth,
-                                 maxlength=displayMaxWidth,
-                                 extra=self.extra)
-        else:
-            return renderElement(self.tag,
-                                 type=self.type,
-                                 name=self.name,
-                                 id=self.name,
-                                 value=self._getFormValue() or '',
-                                 cssClass=self.cssClass,
-                                 style=self.style,
-                                 size=self.displayWidth,
-                                 extra=self.extra)
+        value = self._getFormValue()
+        if value == self._missing:
+            value = ''
 
+        kwargs = {'type': self.type,
+                  'name': self.name,
+                  'id': self.name,
+                  'value': value,
+                  'cssClass': self.cssClass,
+                  'style': self.style,
+                  'size': self.displayWidth,
+                  'extra': self.extra}
+        if self.displayMaxWidth:
+            kwargs['maxlength'] = self.displayMaxWidth # XXX This is untested.
+
+        return renderElement(self.tag, **kwargs)
+
     def _toFieldValue(self, input):
         if self.convert_missing_value and input == self._missing:
             value = self.context.missing_value
@@ -151,7 +145,7 @@
             # We convert everything to unicode. This might seem a bit crude,
             # but anything contained in a TextWidget should be representable
             # as a string. Note that you always have the choice of overriding
-            # the method.  
+            # the method.
             try:
                 value = unicode(input)
             except ValueError, v:
@@ -424,6 +418,20 @@
                 return self.context.missing_value
 
 class IntWidget(TextWidget):
+    """Integer number widget.
+
+    Let's make sure that zeroes are rendered properly:
+
+    >>> from zope.schema import Int
+    >>> field = Int(__name__='foo', title=u'on')
+    >>> widget = IntWidget(field, None)
+    >>> widget.setRenderedValue(0)
+
+    >>> 'value="0"' in widget()
+    True
+
+    """
+
     displayWidth = 10
 
     def _toFieldValue(self, input):



More information about the Zope3-Checkins mailing list