[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/ Added a
required attribute for display widgets so forms can
Jim Fulton
jim at zope.com
Tue May 10 13:47:10 EDT 2005
Log message for revision 30320:
Added a required attribute for display widgets so forms can
highlight fields that are required even if some fields are display
fields.
Also changed some display widgets that sometimes had true required
attributes to always have false required attributes.
Changed:
U Zope3/trunk/src/zope/app/form/browser/itemswidgets.py
U Zope3/trunk/src/zope/app/form/browser/source.py
U Zope3/trunk/src/zope/app/form/browser/source.txt
U Zope3/trunk/src/zope/app/form/browser/tests/test_displaywidget.py
U Zope3/trunk/src/zope/app/form/browser/tests/test_itemswidget.py
U Zope3/trunk/src/zope/app/form/browser/widget.py
U Zope3/trunk/src/zope/app/form/interfaces.py
-=-
Modified: Zope3/trunk/src/zope/app/form/browser/itemswidgets.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/itemswidgets.py 2005-05-10 15:31:34 UTC (rev 30319)
+++ Zope3/trunk/src/zope/app/form/browser/itemswidgets.py 2005-05-10 17:47:10 UTC (rev 30320)
@@ -216,6 +216,10 @@
class ItemDisplayWidget(SingleDataHelper, ItemsWidgetBase):
"""Simple single-selection display that can be used in many cases."""
+ def __init__(self, *args, **kw):
+ ItemsWidgetBase.__init__(self, *args, **kw)
+ self.required = False
+
_messageNoValue = message(_("item-missing-single-value-for-display"), "")
def __call__(self):
@@ -231,6 +235,10 @@
class ItemsMultiDisplayWidget(MultiDataHelper, ItemsWidgetBase):
"""Displays a sequence of items."""
+ def __init__(self, *args, **kw):
+ ItemsWidgetBase.__init__(self, *args, **kw)
+ self.required = False
+
_messageNoValue = message(
_("vocabulary-missing-multiple-value-for-display"), "")
@@ -274,7 +282,6 @@
"""
tag = 'ol'
-
class SetDisplayWidget(ItemsMultiDisplayWidget):
"""Display widget for unordered multi-selection fields.
Modified: Zope3/trunk/src/zope/app/form/browser/source.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/source.py 2005-05-10 15:31:34 UTC (rev 30319)
+++ Zope3/trunk/src/zope/app/form/browser/source.py 2005-05-10 17:47:10 UTC (rev 30320)
@@ -34,7 +34,7 @@
super(SourceDisplayWidget, self).__init__(field, request)
self.source = source
- required = property(lambda self: self.context.required)
+ required = False
def hidden(self):
return ''
Modified: Zope3/trunk/src/zope/app/form/browser/source.txt
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/source.txt 2005-05-10 15:31:34 UTC (rev 30319)
+++ Zope3/trunk/src/zope/app/form/browser/source.txt 2005-05-10 17:47:10 UTC (rev 30320)
@@ -440,6 +440,11 @@
>>> print widget()
tabby
+Like any good display widget, input is not required:
+
+ >>> widget.required
+ False
+
If we specify a list of choices:
>>> pets = zope.schema.List(__name__ = 'pets', title=u"Pets",
Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_displaywidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_displaywidget.py 2005-05-10 15:31:34 UTC (rev 30319)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_displaywidget.py 2005-05-10 17:47:10 UTC (rev 30320)
@@ -46,6 +46,18 @@
'not implemented'
"""
+def test_not_required():
+ """Make sure that display widgets are not required
+
+ >>> field = TextLine(title = u'Title',
+ ... __name__ = u'title',
+ ... default = u'<My Title>')
+ >>> widget = DisplayWidget(field, TestRequest())
+ >>> widget.required
+ False
+
+ """
+
def test_value_escaping():
"""Make sure that the returned values are correctly escaped.
Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_itemswidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_itemswidget.py 2005-05-10 15:31:34 UTC (rev 30319)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_itemswidget.py 2005-05-10 17:47:10 UTC (rev 30320)
@@ -121,6 +121,8 @@
widget = self._makeWidget(form={'field.choice': 'token1'})
self.assertEqual(widget(), 'One')
+ def test_not_required(self):
+ self.failIf(self._makeWidget().required)
class ItemsMultiDisplayWidgetTest(ItemsWidgetBaseTest):
@@ -151,6 +153,15 @@
self.assertRaises(TypeError, widget.renderItems, 1)
+ def test_not_required(self):
+ numbers = List(value_type=ICollector['choice']).bind(Collector(None))
+ request = TestRequest()
+ widget = self._widget(numbers, self._vocabulary, request)
+ self.failIf(widget.required)
+
+
+
+
class ListDisplayWidgetTest(ItemsMultiDisplayWidgetTest):
_widget = ListDisplayWidget
_tag = 'ol'
Modified: Zope3/trunk/src/zope/app/form/browser/widget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/widget.py 2005-05-10 15:31:34 UTC (rev 30319)
+++ Zope3/trunk/src/zope/app/form/browser/widget.py 2005-05-10 17:47:10 UTC (rev 30320)
@@ -390,6 +390,11 @@
class DisplayWidget(BrowserWidget):
+ def __init__(self, context, request):
+ super(DisplayWidget, self).__init__(context, request)
+ self.required = False
+
+
def __call__(self):
if self._renderedValueSet():
value = self._data
@@ -400,7 +405,7 @@
return escape(value)
-class UnicodeDisplayWidget(BrowserWidget):
+class UnicodeDisplayWidget(DisplayWidget):
"""Display widget that converts the value to unicode before display."""
def __call__(self):
Modified: Zope3/trunk/src/zope/app/form/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/form/interfaces.py 2005-05-10 15:31:34 UTC (rev 30319)
+++ Zope3/trunk/src/zope/app/form/interfaces.py 2005-05-10 17:47:10 UTC (rev 30320)
@@ -199,3 +199,10 @@
class IDisplayWidget(IWidget):
"""A widget for displaying a field value."""
+
+ required = Bool(
+ title=u"Required",
+ description=u"""If True, widget should be displayed as requiring input.
+
+ Display widgets should never be required.
+ """)
More information about the Zope3-Checkins
mailing list