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

Fred L. Drake, Jr. fred@zope.com
Fri, 30 May 2003 12:11:02 -0400


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

Modified Files:
	vocabularywidget.py 
Log Message:
Finally really fix the get/have/setData() methods.
These are trickier than they should have to be.


=== Zope3/src/zope/app/browser/form/vocabularywidget.py 1.19 => 1.20 ===
--- Zope3/src/zope/app/browser/form/vocabularywidget.py:1.19	Fri May 30 11:00:36 2003
+++ Zope3/src/zope/app/browser/form/vocabularywidget.py	Fri May 30 12:10:31 2003
@@ -25,7 +25,7 @@
 from zope.app.browser.form import widget
 from zope.app.i18n import ZopeMessageIDFactory as _
 from zope.app.interfaces.browser.form import IVocabularyQueryView
-from zope.app.interfaces.form import WidgetInputError
+from zope.app.interfaces.form import WidgetInputError, MissingInputError
 from zope.publisher.browser import BrowserView
 from zope.component import getView
 from zope.schema.interfaces import IIterableVocabulary, IVocabularyQuery
@@ -163,6 +163,28 @@
         raise NotImplementedError(
             "render() must be implemented by a subclass")
 
+    # The *Data() methods have tightly bound semantics.  Subclasses
+    # need to be really careful about dealing with these, and should
+    # enlist this version for help whenever possible to make sure
+    # internal state is maintained.
+
+    _have_field_data = False
+
+    def getData(self, optional=0):
+        if self._have_field_data:
+            return super(VocabularyWidgetBase, self).getData()
+        elif self.context.required:
+            raise MissingInputError()
+
+    def haveData(self):
+        if self.name in self.request.form:
+            self._have_field_data = True
+        return self._have_field_data
+
+    def setData(self, value):
+        super(VocabularyWidgetBase, self).setData(value)
+        self._have_field_data = True
+
 
 class VocabularyDisplayWidget(VocabularyWidgetBase):
     """Simple single-selection display that can be used in many cases."""
@@ -393,9 +415,6 @@
 
 class VocabularyMultiEditWidget(VocabularyEditWidgetBase):
     """Vocabulary-backed widget supporting multiple selections."""
-
-    def haveData(self):
-        return True
 
     def renderItems(self, value):
         if value == self._missing: