[Zope3-checkins] CVS: Zope3/src/zope/app/browser/form - vocabularywidget.py:1.41

Fred L. Drake, Jr. fred@zope.com
Mon, 16 Jun 2003 17:53:12 -0400


Update of /cvs-repository/Zope3/src/zope/app/browser/form
In directory cvs.zope.org:/tmp/cvs-serv28361

Modified Files:
	vocabularywidget.py 
Log Message:
Improve widget support for non-required single-selection vocabulary fields.

=== Zope3/src/zope/app/browser/form/vocabularywidget.py 1.40 => 1.41 ===
--- Zope3/src/zope/app/browser/form/vocabularywidget.py:1.40	Tue Jun 10 16:06:19 2003
+++ Zope3/src/zope/app/browser/form/vocabularywidget.py	Mon Jun 16 17:52:41 2003
@@ -124,6 +124,9 @@
     return msgid
 
 
+_msg_no_value = message(_("vocabulary-no-value"), "(no value)")
+
+
 # Widget implementation:
 
 class ViewSupport(object, TranslationHook):
@@ -283,11 +286,9 @@
 class VocabularyDisplayWidget(SingleDataHelper, VocabularyWidgetBase):
     """Simple single-selection display that can be used in many cases."""
 
-    _msg_no_value = message(_("vocabulary-no-value"), "(no value)")
-
     def render(self, value):
         if value is None:
-            return self.translate(self._msg_no_value)
+            return self.translate(_msg_no_value)
         else:
             term = self.context.vocabulary.getTerm(value)
             return self.textForValue(term)
@@ -499,7 +500,7 @@
                                     size=self.getValue('size'))
 
     def renderItems(self, value):
-        vocabulary = self.context
+        vocabulary = self.context.vocabulary
         # check if we want to select first item
         if (value == self._missing
             and getattr(self.context, 'firstItem', False)
@@ -510,7 +511,12 @@
             values = [value]
         else:
             values = ()
-        return self.renderItemsWithValues(values)
+        L = self.renderItemsWithValues(values)
+        if not self.context.required:
+            option = ("<option name='%s' value=''>%s</option>"
+                      % (self.name, self.translate(_msg_no_value)))
+            L.insert(0, option)
+        return L
 
 # more general alias
 VocabularyEditWidget = SelectListWidget