Class
zope.app.form.browser.widget.BrowserWidget

Base class for browser widgets.

>>> setUp()

The class provides some basic functionality common to all browser widgets.

Browser widgets have a required attribute, which indicates whether or not the underlying field requires input. By default, the widget's required attribute is equal to the field's required attribute:

>>> from zope.schema import Field
>>> from zope.publisher.browser import TestRequest
>>> field = Field(required=True)
>>> widget = BrowserWidget(field, TestRequest())
>>> widget.required
True
>>> field.required = False
>>> widget = BrowserWidget(field, TestRequest())
>>> widget.required
False

However, the two required values are independent of one another:

>>> field.required = True
>>> widget.required
False

Browser widgets have an error state, which can be rendered in a form using the error() method. The error method delegates the error rendering to a view that is registered as providing IWidgetInputErrorView. To illustrate, we can create and register a simple error display view:

>>> from zope.app.form.interfaces import IWidgetInputError
>>> class SnippetErrorView:
...     implements(IWidgetInputErrorView)
...     def __init__(self, context, request):
...         self.context = context
...     def snippet(self):
...         return "The error: " + str(self.context.errors)
>>> from zope.app.testing import ztapi
>>> ztapi.browserViewProviding(IWidgetInputError, SnippetErrorView,
...                            IWidgetInputErrorView)

Whever an error occurs, widgets should set _error:

>>> widget._error = WidgetInputError('foo', 'Foo', ('Err1', 'Err2'))

so that it can be displayed using the error() method:

>>> widget.error()
"The error: ('Err1', 'Err2')"
>>> tearDown()

Base classes

Implemented Interfaces

Attributes/Properties

Methods

Known Subclasses