[Checkins] SVN: z3c.widget/trunk/src/z3c/widget/optdropdown/ Okay, ironed out some problems. The widget works correctly now.

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Dec 28 22:28:18 EST 2006


Log message for revision 71668:
  Okay, ironed out some problems. The widget works correctly now.
  

Changed:
  U   z3c.widget/trunk/src/z3c/widget/optdropdown/README.txt
  U   z3c.widget/trunk/src/z3c/widget/optdropdown/configure.zcml
  U   z3c.widget/trunk/src/z3c/widget/optdropdown/widget.py
  U   z3c.widget/trunk/src/z3c/widget/optdropdown/z3c.widget.optdropdown-configure.zcml

-=-
Modified: z3c.widget/trunk/src/z3c/widget/optdropdown/README.txt
===================================================================
--- z3c.widget/trunk/src/z3c/widget/optdropdown/README.txt	2006-12-29 03:04:02 UTC (rev 71667)
+++ z3c.widget/trunk/src/z3c/widget/optdropdown/README.txt	2006-12-29 03:28:18 UTC (rev 71668)
@@ -339,6 +339,7 @@
   <div class="value">
   <select id="field.occupation.occupation"
           name="field.occupation.occupation" size="1" >
+  <option selected="selected" value="">(no value)</option>
   <option value="Programmer">Programmer</option>
   <option value="Designer">Designer</option>
   <option value="Project Manager">Project Manager</option>

Modified: z3c.widget/trunk/src/z3c/widget/optdropdown/configure.zcml
===================================================================
--- z3c.widget/trunk/src/z3c/widget/optdropdown/configure.zcml	2006-12-29 03:04:02 UTC (rev 71667)
+++ z3c.widget/trunk/src/z3c/widget/optdropdown/configure.zcml	2006-12-29 03:28:18 UTC (rev 71668)
@@ -4,9 +4,9 @@
 
   <view
       type="zope.publisher.interfaces.browser.IBrowserRequest"
-      for="z3c.schema.optchoice.IOptionalCoice"
+      for="z3c.schema.optchoice.IOptionalChoice"
       provides="zope.app.form.interfaces.IInputWidget"
-      factory=".widget.IOptionalDropDown"
+      factory=".widget.OptionalDropdownWidget"
       permission="zope.Public"
       />
 

Modified: z3c.widget/trunk/src/z3c/widget/optdropdown/widget.py
===================================================================
--- z3c.widget/trunk/src/z3c/widget/optdropdown/widget.py	2006-12-29 03:04:02 UTC (rev 71667)
+++ z3c.widget/trunk/src/z3c/widget/optdropdown/widget.py	2006-12-29 03:28:18 UTC (rev 71668)
@@ -17,8 +17,10 @@
 """
 __docformat__ = "reStructuredText"
 import zope.component
+import zope.schema.interfaces
 from zope.app import form
 from zope.app.form import browser
+from zope.app.form.interfaces import MissingInputError
 
 class OptionalDropdownWidget(object):
     """Optional Dropdown Widget"""
@@ -41,13 +43,17 @@
     def __init__(self, field, request):
         self.context = field
         self.request = request
+        # Clone field again, because we change the ``require`` options
+        clone = field.bind(field.context)
+        clone.required = False
+        clone.value_type.required = False
         # Setup the custom value widget
-        field.value_type.__name__ = 'custom'
+        clone.value_type.__name__ = 'custom'
         self.customWidget = zope.component.getMultiAdapter(
-            (field.value_type, request), form.interfaces.IInputWidget)
+            (clone.value_type, request), form.interfaces.IInputWidget)
         # Setup the dropdown widget
         self.dropdownWidget = form.browser.DropdownWidget(
-            field, field.vocabulary, request)
+            clone, clone.vocabulary, request)
         # Setting the prefix again, sets everything up correctly
         self.setPrefix(self._prefix)
 
@@ -74,11 +80,20 @@
 
     def getInputValue(self):
         """See zope.app.form.interfaces.IInputWidget"""
-        if self.customWidget.hasInput():
+        customMissing = self.context.value_type.missing_value
+        if (self.customWidget.hasInput() and
+            self.customWidget.getInputValue() != customMissing):
             return self.customWidget.getInputValue()
-        else:
+
+        dropdownMissing = self.context.value_type.missing_value
+        if (self.dropdownWidget.hasInput() and
+            self.dropdownWidget.getInputValue() != dropdownMissing):
             return self.dropdownWidget.getInputValue()
 
+        raise MissingInputError(self.name, self.label,
+                                zope.schema.interfaces.RequiredMissing())
+
+
     def applyChanges(self, content):
         """See zope.app.form.interfaces.IInputWidget"""
         field = self.context
@@ -96,8 +111,23 @@
 
     def hasValidInput(self):
         """See zope.app.form.interfaces.IInputWidget"""
-        return (self.dropdownWidget.hasValidInput() or
-                self.customWidget.hasValidInput())
+        customValid = self.customWidget.hasValidInput()
+        customMissing = self.context.value_type.missing_value
+        dropdownValid = self.dropdownWidget.hasValidInput()
+        dropdownMissing = self.context.missing_value
+        # If the field is required and both values are missing, then the input
+        # is invalid
+        if self.context.required:
+            return (
+                (customValid and
+                 self.customWidget.getInputValue() != customMissing)
+                or
+                (dropdownValid and
+                 self.dropdownWidget.getInputValue() != dropdownMissing)
+                )
+        # If the field is not required, we just need either input to be valid,
+        # since both generated widgets have non-required fields.
+        return customValid or dropdownValid
 
     def hidden(self):
         """See zope.app.form.browser.interfaces.IBrowserWidget"""

Modified: z3c.widget/trunk/src/z3c/widget/optdropdown/z3c.widget.optdropdown-configure.zcml
===================================================================
--- z3c.widget/trunk/src/z3c/widget/optdropdown/z3c.widget.optdropdown-configure.zcml	2006-12-29 03:04:02 UTC (rev 71667)
+++ z3c.widget/trunk/src/z3c/widget/optdropdown/z3c.widget.optdropdown-configure.zcml	2006-12-29 03:28:18 UTC (rev 71668)
@@ -1 +1 @@
-<include package="z3c.widget.flashupload" />
\ No newline at end of file
+<include package="z3c.widget.optdropdown" />



More information about the Checkins mailing list