Hi All,

I've had a bit of a struggle getting formlib error views to play nicely
with invariants. That is the problem I have found to trouble me is in
the zope.formlib.form.FormBase method error_views.

When I use the schema:

class IMemberRegisterForm(IMemberData, IMemberDetails):
    """Schema for a member register form."""

    new_password = Password(
        title=_("Choose a Password"),

    verify_password = Password(
        title=_("Verify Password"),

    def passwordsMatch(register):
        if register.new_password != register.verify_password:
            msg = _("Entered passwords do not match")
            error = ('verify_password', _("Passwords"), msg)
            raise Invalid(error)

As far as I can ascertain I must raise the Invalid exception so that it
is caught correctly in zope.interface.interface.InterfaceClass
validateInvariants method.

Now when the FormBase error_views method is called there is no
multiadapter for the Invalid exception. First option then is to create a
view for Invalid, I passed on that, and instead redefined error_views in
my view class with the following:

    def error_views(self):
        for error in self.errors:
            if isinstance(error, basestring):
                yield error
                if not isinstance(error, WidgetInputError):
                    e = list(error)[0]
                    error = WidgetInputError(e[0], e[1], e[2])
                if isinstance(error, WidgetInputError):
                    view = getMultiAdapter(
                        (error, self.request), IWidgetInputErrorView)
                    title = getattr(error, 'widget_title', None)
                    if title:
                        yield '%s: %s' % (title, view.snippet())
                        yield view.snippet()

There must be a cleaner way to do the same. That is to convert the
Invalid error to a WidgetInputError.

Has anyone done better? I am sure.

Best regards,

Zope3-users mailing list

Reply via email to