[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/source.
Jim: Fixed bugs in checking for and getting input values.
Stephan Richter
srichter at cosmos.phy.tufts.edu
Wed Oct 13 10:55:42 EDT 2004
Log message for revision 28090:
Jim: Fixed bugs in checking for and getting input values.
Changed:
U Zope3/trunk/src/zope/app/form/browser/source.py
U Zope3/trunk/src/zope/app/form/browser/source.txt
-=-
Modified: Zope3/trunk/src/zope/app/form/browser/source.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/source.py 2004-10-13 14:54:23 UTC (rev 28089)
+++ Zope3/trunk/src/zope/app/form/browser/source.py 2004-10-13 14:55:41 UTC (rev 28090)
@@ -15,10 +15,9 @@
$Id$
"""
-
import cgi
import zope.schema.interfaces
-from zope.schema.interfaces import ISourceQueriables
+from zope.schema.interfaces import ISourceQueriables, ValidationError
from zope.app import zapi
import zope.app.form.interfaces
import zope.app.form.browser.widget
@@ -208,12 +207,19 @@
required = property(lambda self: self.context.required)
def getInputValue(self):
- token = self.request.get(self.name)
+ for name, queryview in self.queryviews:
+ if name+'.apply' in self.request:
+ token = self.request.form.get(name+'.selection')
+ if token is not None:
+ break
+ else:
+ token = self.request.get(self.name)
+
field = self.context
if token is None:
if field.required:
- raise zope.app.form.interfacesMissingInputError(
+ raise zope.app.form.interfaces.MissingInputError(
field.__name__, self.label,
)
return field.missing_value
@@ -237,39 +243,51 @@
return value
def hasInput(self):
- return self.name+'.displayed' in self.request.form
+ if self.name in self.request:
+ return True
+ if (not self.context.required and
+ self.name+'.displayed' in self.request):
+ return True
+ for name, queryview in self.queryviews:
+ if name+'.apply' in self.request:
+ token = self.request.form.get(name+'.selection')
+ if token is not None:
+ return True
+
+ return False
+
class SourceListInputWidget(SourceInputWidget):
- def _value(self):
- if self._renderedValueSet():
- value = self._data
+ def _input_value(self):
+ tokens = self.request.form.get(self.name)
+ for name, queryview in self.queryviews:
+ if name+'.apply' in self.request:
+ newtokens = self.request.form.get(name+'.selection')
+ if newtokens:
+ if tokens:
+ tokens = tokens + newtokens
+ else:
+ tokens = newtokens
+
+ if tokens:
+ remove = self.request.form.get(self.name+'.checked')
+ if remove and (self.name+'.remove' in self.request):
+ tokens = [token
+ for token in tokens
+ if token not in remove
+ ]
+ value = []
+ for token in tokens:
+ try:
+ v = self.terms.getValue(str(token))
+ except LookupError:
+ pass # skip invalid tokens (shrug)
+ else:
+ value.append(v)
else:
- tokens = self.request.form.get(self.name)
- for name, queryview in self.queryviews:
- if name+'.apply' in self.request:
- newtokens = self.request.form.get(name+'.selection')
- if newtokens:
- if tokens:
- tokens = tokens + newtokens
- else:
- tokens = newtokens
-
- if tokens:
- remove = self.request.form.get(self.name+'.checked')
- if remove and (self.name+'.remove' in self.request):
- tokens = [token
- for token in tokens
- if token not in remove
- ]
+ if self.name+'.displayed' in self.request:
value = []
- for token in tokens:
- try:
- v = self.terms.getValue(str(token))
- except LookupError:
- pass # skip invalid tokens (shrug)
- else:
- value.append(v)
else:
value = self.context.missing_value
@@ -283,6 +301,14 @@
value = r
return value
+
+ def _value(self):
+ if self._renderedValueSet():
+ value = self._data
+ else:
+ value = self._input_value()
+
+ return value
def hidden(self):
value = self._value()
@@ -373,33 +399,18 @@
)
def getInputValue(self):
- tokens = self.request.get(self.name)
- field = self.context
-
- if not tokens:
- if field.required:
- raise zope.app.form.interfacesMissingInputError(
- field.__name__, self.label,
- )
- return field.missing_value
-
- value = []
- for token in tokens:
- try:
- v = self.terms.getValue(str(token))
- except LookupError:
- err = zope.schema.interfaces.ValidationError(
- "Invalid value id", token)
- raise WidgetInputError(field.__name__, self.label, err)
- value.append(v)
+ value = self._input_value()
# Remaining code copied from SimpleInputWidget
# value must be valid per the field constraints
try:
- field.validate(value)
+ self.context.validate(value)
except ValidationError, err:
self._error = WidgetInputError(field.__name__, self.label, err)
raise self._error
return value
+
+ def hasInput(self):
+ return self.name+'.displayed' in self.request.form
Modified: Zope3/trunk/src/zope/app/form/browser/source.txt
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/source.txt 2004-10-13 14:54:23 UTC (rev 28089)
+++ Zope3/trunk/src/zope/app/form/browser/source.txt 2004-10-13 14:55:41 UTC (rev 28090)
@@ -149,6 +149,34 @@
>>> request.form['field.dog.query.string'] = u'o'
>>> request.form['field.dog.query'] = u'Search'
+Because the field is required, a non-selection is not valid. Thus, the widget
+does not have an input value.
+
+ >>> widget.hasInput()
+ False
+
+If the field is not required:
+
+ >>> dog.required = False
+
+then as long as the field is displayed, the widget says it has input.
+
+ >>> widget.hasInput()
+ True
+
+ >>> dog.required = True
+
+If we actually provide some input:
+
+ >>> request.form['field.dog'] = 'foo'
+
+then we have input:
+
+ >>> widget.hasInput()
+ True
+
+ >>> del request.form['field.dog']
+
Now if we render the widget, we'll see the search results:
>>> print widget()
@@ -199,6 +227,17 @@
</div> <!-- queries -->
</div> <!-- value -->
+Note that we should have an input value now, since pressing the 'Apply' button
+provides us with input.
+
+ >>> widget.hasInput()
+ True
+
+We should also be able to get the input value:
+
+ >>> widget.getInputValue()
+ 'spot'
+
Now, let's look at a more complicated example. We'll define a source
that combines multiple sources:
@@ -341,6 +380,17 @@
</div> <!-- queries -->
</div> <!-- value -->
+Note that we should have an input value now, since pressing the 'Apply' button
+provides us with input.
+
+ >>> widget.hasInput()
+ True
+
+and we can get the input value:
+
+ >>> widget.getInputValue()
+ 'tabby'
+
There's a display widget, which doesn't use queriables, since it
doesn't assign values:
@@ -472,6 +522,11 @@
</div> <!-- queries -->
</div> <!-- value -->
+We can get the selected values:
+
+ >>> widget.getInputValue()
+ ['tabby', 'tiger', 'tom']
+
We now see the values we selected. We also have chackboxes and
buttons that allow is to remove selections:
More information about the Zope3-Checkins
mailing list