[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/form/browser/ Minor
improvement to simple input widget that lets a subclass
define its own concept of form 'input'. Changes are currently
used checkbox widget, which uses two form elements.
Garrett Smith
garrett at mojave-corp.com
Thu Jun 10 12:50:37 EDT 2004
Log message for revision 25334:
Minor improvement to simple input widget that lets a subclass define its own concept of form 'input'. Changes are currently used checkbox widget, which uses two form elements.
-=-
Modified: Zope3/trunk/src/zope/app/form/browser/boolwidgets.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/boolwidgets.py 2004-06-10 14:32:54 UTC (rev 25333)
+++ Zope3/trunk/src/zope/app/form/browser/boolwidgets.py 2004-06-10 16:50:37 UTC (rev 25334)
@@ -29,9 +29,7 @@
For more detailed documentation, including sample code, see
tests/test_checkboxwidget.py.
- """
- implements(IInputWidget)
-
+ """
type = 'checkbox'
default = 0
extra = ''
@@ -39,7 +37,7 @@
def __call__(self):
"""Render the widget to HTML."""
value = self._getFormValue()
- if value:
+ if value == 'on':
kw = {'checked': None}
else:
kw = {}
@@ -51,12 +49,12 @@
value=""
),
renderElement(self.tag,
- type=self.type,
- name=self.name,
- id=self.name,
- cssClass=self.cssClass,
- extra=self.extra,
- **kw),
+ type=self.type,
+ name=self.name,
+ id=self.name,
+ cssClass=self.cssClass,
+ extra=self.extra,
+ **kw),
)
def _toFieldValue(self, input):
@@ -65,22 +63,31 @@
def _toFormValue(self, value):
"""Convert from Python bool to HTML representation."""
- return value and "on" or ""
+ return value and 'on' or ''
def hasInput(self):
"""Check whether the field is represented in the form."""
return self.name + ".used" in self.request.form or \
super(CheckBoxWidget, self).hasInput()
- def getInputValue(self):
- """Get the value from the form
+ def _getFormInput(self):
+ """Returns the form input used by _toFieldValue.
- When it's checked, its value is 'on'.
- When a checkbox is unchecked, it does not appear in the form data."""
- value = self.request.form.get(self.name, 'off')
- return value == 'on'
+ Return values:
+
+ 'on' checkbox is checked
+ '' checkbox is not checked
+ None form input was not provided
+ """
+ if self.request.get(self.name) == 'on':
+ return 'on'
+ elif self.name + '.used' in self.request:
+ return ''
+ else:
+ return None
+
def BooleanRadioWidget(field, request, true=_('on'), false=_('off')):
vocabulary = SimpleVocabulary.fromItems( ((True, true), (False, false)) )
return RadioWidget(field, vocabulary, request)
Modified: Zope3/trunk/src/zope/app/form/browser/tests/test_checkboxwidget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/tests/test_checkboxwidget.py 2004-06-10 14:32:54 UTC (rev 25333)
+++ Zope3/trunk/src/zope/app/form/browser/tests/test_checkboxwidget.py 2004-06-10 16:50:37 UTC (rev 25334)
@@ -22,6 +22,7 @@
from zope.schema import Bool
from zope.interface.verify import verifyClass
+from zope.app.form.interfaces import MissingInputError
from zope.app.form.browser.tests.test_browserwidget import SimpleInputWidgetTest
@@ -137,7 +138,10 @@
self._widget.request.form['field.foo'] = 'positive'
self.assertEqual(self._widget.getInputValue(), False)
del self._widget.request.form['field.foo']
- self.assertEqual(self._widget.getInputValue(), False)
+ self._widget.request.form['field.foo.used'] = ''
+ self.assertEquals(self._widget.getInputValue(), False)
+ del self._widget.request.form['field.foo.used']
+ self.assertRaises(MissingInputError, self._widget.getInputValue)
def test_suite():
Modified: Zope3/trunk/src/zope/app/form/browser/widget.py
===================================================================
--- Zope3/trunk/src/zope/app/form/browser/widget.py 2004-06-10 14:32:54 UTC (rev 25333)
+++ Zope3/trunk/src/zope/app/form/browser/widget.py 2004-06-10 16:50:37 UTC (rev 25334)
@@ -106,7 +106,7 @@
class SimpleInputWidget(BrowserWidget, InputWidget):
- """A widget that uses a single HTML form element to capture user input.
+ """A baseclass for simple HTML form widgets.
>>> setUp()
@@ -255,12 +255,11 @@
field = self.context
# form input is required, otherwise raise an error
- input = self.request.form.get(self.name)
- if input is None:
+ if not self.hasInput():
raise MissingInputError(self.name, self.label, None)
# convert input to suitable value - may raise conversion error
- value = self._toFieldValue(input)
+ value = self._toFieldValue(self._getFormInput())
# allow missing values only for non-required fields
if value == field.missing_value and not field.required:
@@ -284,6 +283,19 @@
else:
return False
+ def _getFormInput(self):
+ """Returns current form input.
+
+ The value returned must be in a format that can be used as the 'input'
+ argument to _toFieldValue.
+
+ The default implementation returns the form value that corresponds to
+ the widget's name. Subclasses may override this method if their form
+ input consists of more than one form element or use an alternative
+ naming convention.
+ """
+ return self.request.get(self.name)
+
def _toFieldValue(self, input):
"""Converts input to a value appropriate for the field type.
@@ -291,10 +303,8 @@
perform an appropriate conversion.
This method is used by getInputValue to perform the conversion
- of a form input value (keyed by the widget's name) to an appropriate
- field value. Widgets that require a more complex conversion process
- (e.g. utilize more than one form field) should override getInputValue
- and disregard this method.
+ of form input (provided by _getFormInput) to an appropriate field
+ value.
"""
if input == self._missing:
return self.context.missing_value
More information about the Zope3-Checkins
mailing list