Class
zope.app.form.browser.widget.SimpleInputWidget

A baseclass for simple HTML form widgets.

>>> setUp()

Simple input widgets read input from a browser form. To illustrate, we will use a test request with two form values:

>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest(form={
...     'field.foo': u'hello\r\nworld',
...     'baz.foo': u'bye world'})

Like all widgets, simple input widgets are a view to a field context:

>>> from zope.schema import Field
>>> field = Field(__name__='foo', title=u'Foo')
>>> widget = SimpleInputWidget(field, request)

Widgets are named using their field's name:

>>> widget.name
'field.foo'

The default implementation for the widget label is to use the field title:

>>> widget.label
u'Foo'

According the request, the widget has input because 'field.foo' is present:

>>> widget.hasInput()
True
>>> widget.getInputValue()
u'hello\r\nworld'

Widgets maintain an error state, which is used to communicate invalid input or other errors:

>>> widget._error is None
True
>>> widget.error()
''

setRenderedValue is used to specify the value displayed by the widget to the user. This value, however, is not the same as the input value, which is read from the request:

>>> widget.setRenderedValue('Hey\nfolks')
>>> widget.getInputValue()
u'hello\r\nworld'
>>> widget._error is None
True
>>> widget.error()
''

You can use 'setPrefix' to remove or modify the prefix used to create the widget name as follows:

>>> widget.setPrefix('')
>>> widget.name
'foo'
>>> widget.setPrefix('baz')
>>> widget.name
'baz.foo'

getInputValue always returns a value that can legally be assigned to the widget field. To illustrate widget validation, we can add a constraint to its field:

>>> import re
>>> field.constraint = re.compile('.*hello.*').match

Because we modified the widget's name, the widget will now read different form input:

>>> request.form[widget.name]
u'bye world'

This input violates the new field constraint and therefore causes an error when getInputValue is called:

>>> widget.getInputValue()
Traceback (most recent call last):
WidgetInputError: ('foo', u'Foo', bye world)

Simple input widgets require that input be available in the form request. If input is not present, a MissingInputError is raised:

>>> del request.form[widget.name]
>>> widget.getInputValue()
Traceback (most recent call last):
MissingInputError: ('baz.foo', u'Foo', None)

A MissingInputError indicates that input is missing from the form altogether. It does not indicate that the user failed to provide a value for a required field. The MissingInputError above was caused by the fact that the form does have any input for the widget:

>>> request.form[widget.name]
Traceback (most recent call last):
KeyError: 'baz.foo'

If a user fails to provide input for a field, the form will contain the input provided by the user, namely an empty string:

>>> request.form[widget.name] = ''

In such a case, if the field is required, a WidgetInputError will be raised on a call to getInputValue:

>>> field.required = True
>>> widget.getInputValue()
Traceback (most recent call last):
WidgetInputError: ('foo', u'Foo', )

However, if the field is not required, the empty string will be converted by the widget into the field's missing_value and read as a legal field value:

>>> field.required = False
>>> widget.getInputValue() is field.missing_value
True

Another type of exception is a conversion error. It is raised when a value cannot be converted to the desired Python object. Here is an example of a floating point.

>>> from zope.schema import Float
>>> field = Float(__name__='price', title=u'Price')
>>> from zope.app.form.interfaces import ConversionError
>>> class FloatWidget(SimpleInputWidget):
...     def _toFieldValue(self, input):
...         try:
...             return float(input)
...         except ValueError, v:
...             raise ConversionError('Invalid floating point data', v)
...
...     def _toFormValue(self, value):
...         value = super(FloatWidget, self)._toFormValue(value)
...         return '%.2f' % value
>>> request = TestRequest(form={'field.price': u'32.0'})
>>> widget = FloatWidget(field, request)
>>> widget.getInputValue()
32.0
>>> widget()
u'<input class="textType" id="field.price" name="field.price" type="text" value="32.00"  />'
>>> request = TestRequest(form={'field.price': u'<p>foo</p>'})
>>> widget = FloatWidget(field, request)
>>> try:
...     widget.getInputValue()
... except ConversionError, error:
...     print error.doc()
Invalid floating point data
>>> widget()
u'<input class="textType" id="field.price" name="field.price" type="text" value="&lt;p&gt;foo&lt;/p&gt;"  />'
>>> tearDown()

Base classes

Implemented Interfaces

Attributes/Properties

Methods

Known Subclasses