[Zope3-dev] Missing widget data and error handling

Marius Gedminas mgedmin at b4net.lt
Tue Jul 27 12:18:23 EDT 2004


Hi, form experts!

I've just discovered what I think is a small bug in the form machinery
(zope.app.form.utility.getWidgetsData, to be precise).

Imagine that you have a form.  Normally, if the user submits a request
with an invalid value for a form field, she gets back the same form with
the error displayed next to the field.  Internally, this happens because
widget.getInputValue() does some validation and assigns an exception
object to self._error.  The form rendering code sees that widget.error()
is not None and renders it nicely next to the widget itself.

However imagine that the value for a field is missing, instead of simply
being invalid.  By missing I mean that request.form does not contain
'field.name' at all, as opposed to containing a 'field.name' with an
empty (u"") value.  In this case getWidgetsData never calls
widget.getInputValue(), therefore widget.error() remains None, and the
form is rendered without an error message next to the field.

Usually this does not happen.  Most input widgets (text lines, drop down
boxes, etc.) always cause the browser to submit a field in the request,
even if the value is empty.  Some input widgets do not.  One particular
instance is RadioWidget -- if none of the radio buttons is selected, the
browser will not send that input field when submitting the form.  As a
result, a rendered RadioWidget never displays the error message saying
that required input is missing.

One workaround is to set widget._error explicitly in getWidgetsData so that a
missing value would cause widget.error() to return something.  However it is
not correct, because an IInputWidget is not required to have an attribute
called '_error', assignment to which would cause error() to return the same
error.

Another idea -- to always call getInputValue and let it raise an error -- fails
to solve the problem because some widgets (e.g. TextWidget, RadioWidget) do not
set self._error if self.hasInput() returns False.

Does anyone have other ideas?  Should I file a bug report in the collector?

Marius Gedminas
-- 
Microsoft's entry in this cavalcade of horrors is Universal Plug and Play
(UPnP).  This is a protocol that allows [...] an end-user system to request a
dynamic port-forwarding from the firewall to the box.  Many network
administrators will probably (rightly) recoil at letting applications on a
Windows box dictate firewall policy.
        -- Anthony Baxter
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-dev/attachments/20040727/a069278b/attachment.bin


More information about the Zope3-dev mailing list