[Checkins] SVN: zc.form/trunk/src/zc/form/browser/co - added
display widget for combination fields
Christian Theune
ct at gocept.com
Mon Jan 22 09:26:17 EST 2007
Log message for revision 72173:
- added display widget for combination fields
- updated doctest to rest conformance
Changed:
U zc.form/trunk/src/zc/form/browser/combinationwidget.py
U zc.form/trunk/src/zc/form/browser/combinationwidget.txt
U zc.form/trunk/src/zc/form/browser/configure.zcml
-=-
Modified: zc.form/trunk/src/zc/form/browser/combinationwidget.py
===================================================================
--- zc.form/trunk/src/zc/form/browser/combinationwidget.py 2007-01-22 12:27:57 UTC (rev 72172)
+++ zc.form/trunk/src/zc/form/browser/combinationwidget.py 2007-01-22 14:26:16 UTC (rev 72173)
@@ -23,7 +23,7 @@
import zope.schema.interfaces
import zope.cachedescriptors.property
from zope.app.form.interfaces import WidgetInputError
-from zope.app.form.interfaces import IInputWidget
+from zope.app.form.interfaces import IInputWidget, IDisplayWidget
from zope.app.pagetemplate import ViewPageTemplateFile
from zope.formlib import namedtemplate
@@ -33,13 +33,16 @@
class CombinationWidget(BaseWidget):
+ widget_interface = IInputWidget
+
@zope.cachedescriptors.property.Lazy
def widgets(self):
field = self.context
res = []
for f in field.fields:
f = f.bind(self.context)
- w = component.getMultiAdapter((f, self.request,), IInputWidget)
+ w = component.getMultiAdapter((f, self.request,),
+ self.widget_interface)
w.setPrefix(self.name + ".")
res.append(w)
return res
@@ -48,7 +51,7 @@
super(CombinationWidget, self).setPrefix(prefix)
for w in self.widgets:
w.setPrefix(self.name + ".")
-
+
def loadValueFromRequest(self):
# the lack of an API to get the input value regardless of validation
# is a significant problem. The inability to clear errors is a
@@ -91,9 +94,9 @@
else:
values = tuple(values)
return values
-
+
template = namedtemplate.NamedTemplate('default')
-
+
def render(self, value):
field = self.context
missing_value = field.missing_value
@@ -119,3 +122,8 @@
default_template = namedtemplate.NamedTemplateImplementation(
ViewPageTemplateFile('combinationwidget.pt'), CombinationWidget)
+
+
+class CombinationDisplayWidget(CombinationWidget):
+
+ widget_interface = IDisplayWidget
Modified: zc.form/trunk/src/zc/form/browser/combinationwidget.txt
===================================================================
--- zc.form/trunk/src/zc/form/browser/combinationwidget.txt 2007-01-22 12:27:57 UTC (rev 72172)
+++ zc.form/trunk/src/zc/form/browser/combinationwidget.txt 2007-01-22 14:26:16 UTC (rev 72173)
@@ -1,10 +1,10 @@
The combinationwidget collects two or more subfields to provide a convenient
way to specify a sequence of values.
-Rendering the widget returns a table with the subfields.
+Rendering the widget returns a table with the subfields::
>>> from zc.form.browser.combinationwidget import (
- ... CombinationWidget, default_template)
+ ... CombinationWidget, CombinationDisplayWidget, default_template)
>>> from zope import component, interface
>>> component.provideAdapter(default_template, name='default')
>>> from zc.form.field import Combination, OrderedCombinationConstraint
@@ -63,9 +63,8 @@
</tr>
</table>
-
Setting the appropriate values in the Request lets the widget correctly read
-the specified value.
+the specified value::
>>> request.form['field.acceptable_count-marker'] = 'x'
>>> request.form['field.acceptable_count.combination_00'] = '10'
@@ -83,8 +82,9 @@
name="field.acceptable_count.combination_01" size="10" type="text"
value="" />...
-The field is fine with empty values, because it is not required.
+The field is fine with empty values, because it is not required::
+
>>> request.form['field.acceptable_count-marker'] = 'x'
>>> request.form['field.acceptable_count.combination_00'] = ''
>>> request.form['field.acceptable_count.combination_01'] = ''
@@ -105,7 +105,7 @@
False
If the optional value is filled in and the required one is not, though, there
-are errors.
+are errors::
>>> request.form['field.acceptable_count-marker'] = 'x'
>>> request.form['field.acceptable_count.combination_00'] = ''
@@ -146,7 +146,7 @@
>>> widget.widgets[0].error()
u'Required input is missing.'
-Similarly, if the field's constraints are not met, the widget shows errors.
+Similarly, if the field's constraints are not met, the widget shows errors::
>>> request.form['field.acceptable_count-marker'] = 'x'
>>> request.form['field.acceptable_count.combination_00'] = '20'
@@ -167,3 +167,42 @@
type="text" value="10" />...
>>> widget.error()
u'${minimum} must be less than or equal to ${maximum}.'
+
+
+There's also a display version of the widget::
+
+ >>> request = TestRequest()
+ >>> from zope.app.form.browser.widget import DisplayWidget
+ >>> from zope.app.form.interfaces import IDisplayWidget
+ >>> ztapi.provideView(
+ ... IInt, IBrowserRequest, IDisplayWidget, '', DisplayWidget)
+ >>> widget = CombinationDisplayWidget(IDemo['acceptable_count'], request)
+ >>> widget.setPrefix('field')
+ >>> widget.setRenderedValue(('10', '2'))
+ >>> print widget() # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE +REPORT_NDIFF
+ <input type='hidden' name='field.acceptable_count-marker' value='x' />
+ <table class="combinationFieldWidget">
+ <tr>
+ <td class="label">
+ <label for="field.acceptable_count.combination_00">
+ <span>Minimum</span>
+ </label>
+ </td>
+ <td class="field">
+ <div class="widget">10
+ </div>
+ </td>
+ </tr>
+ <tr>
+ <td class="label">
+ <label for="field.acceptable_count.combination_01">
+ <span>Maximum</span>
+ </label>
+ </td>
+ <td class="field">
+ <div class="widget">2
+ </div>
+ </td>
+ </tr>
+ </table>
+
Modified: zc.form/trunk/src/zc/form/browser/configure.zcml
===================================================================
--- zc.form/trunk/src/zc/form/browser/configure.zcml 2007-01-22 12:27:57 UTC (rev 72172)
+++ zc.form/trunk/src/zc/form/browser/configure.zcml 2007-01-22 14:26:16 UTC (rev 72173)
@@ -23,6 +23,14 @@
/>
<adapter factory=".combinationwidget.default_template" name="default" />
+ <view
+ type="zope.publisher.interfaces.browser.IBrowserRequest"
+ for="zc.form.interfaces.ICombinationField"
+ provides="zope.app.form.interfaces.IDisplayWidget"
+ factory=".combinationwidget.CombinationDisplayWidget"
+ permission="zope.Public"
+ />
+
<!-- we have Invariant exceptions for which we want to register a "snippet"
view -->
More information about the Checkins
mailing list