Skip to content

Commit

Permalink
Merged bugfix for IntWidget (it would not render 0) from trunk (28918…
Browse files Browse the repository at this point in the history
…:28920).
  • Loading branch information
Gintautas Miliauskas committed Jan 24, 2005
1 parent 577ed77 commit d09788a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion browser/tests/test_intwidget.py
Expand Up @@ -28,7 +28,7 @@

class IntWidgetTest(SimpleInputWidgetTest):
"""Documents and tests the int widget.
>>> verifyClass(IInputWidget, IntWidget)
True
"""
Expand Down
2 changes: 1 addition & 1 deletion browser/tests/test_textwidget.py
Expand Up @@ -120,7 +120,7 @@ def test_w_nonrequired_and_missing_value_and_no_inout():
"""
There was a bug that caused the value attribute to be set to
'value' under these circumstances.
>>> from zope.publisher.browser import TestRequest
>>> from zope.schema import TextLine
>>> field = TextLine(__name__='foo', title=u'on',
Expand Down
54 changes: 31 additions & 23 deletions browser/textwidgets.py
Expand Up @@ -120,28 +120,22 @@ def __init__(self, *args):
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 is None or value == self.context.missing_value:
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:
Expand All @@ -150,7 +144,7 @@ def _toFieldValue(self, input):
# 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:
Expand Down Expand Up @@ -389,6 +383,20 @@ def _toFieldValue(self, input):
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):
Expand Down
4 changes: 2 additions & 2 deletions browser/widget.py
Expand Up @@ -296,7 +296,7 @@ def getInputValue(self):
except ConversionError, error:
# ConversionError is already a WidgetInputError
self._error = error
raise self._error
raise self._error

# allow missing values only for non-required fields
if value == field.missing_value and not field.required:
Expand Down Expand Up @@ -448,7 +448,7 @@ def renderTag(tag, **kw):
items = kw.items()
items.sort()
for key, value in items:
if value == None:
if value is None:
value = key
attr_list.append(u'%s=%s' % (key, quoteattr(unicode(value))))

Expand Down

0 comments on commit d09788a

Please sign in to comment.