[Zope3-Users] Invariants and forms

NYLAN at gmx.net NYLAN at gmx.net
Mon Aug 13 05:37:19 EDT 2007


Hello, I'm using an invariant to check if given passwords are equal.

I've found two different examples(see below), but both don't display my given exception message. The only thing i get is the standard error message.

Since this is my first experience with zope, could someone explain to me the correct way to get a custom error message with invariants?

Thanks!!!
I'm using zope version 3.3.1.

First example
(taken from: http://blog.gocept.com/zope3-testen-von-felderuebergreifenden-bedingungen-in-interfaces-mit-zope-formlib/):

class PasswordsAreNotEqual(zope.schema.ValidationError):
    u"""Das Passwort und die Wiederholung sind nicht gleich."""
    zope.interface.implements(zope.app.form.interfaces.IWidgetInputError)

def arePasswordsEqual(obj)
    if obj.password != obj.password2:
        raise PasswordsAreNotEqual

from zope.interface import Interface, invariant

class IUser(Interface):
    password = zope.schema.Password(title=u"Passwort")
    password2 = zope.schema.Password(title=u"Wiederholung des Passworts")

    arePasswordsEqual = invariant(arePasswordsEqual)


Second example
(taken from:
http://blog.gocept.com/zope3-testen-von-felderuebergreifenden-bedingungen-invariants-in-interfaces-mit-zope-formlib-aktualisiert)

import zope.interface

class IUser(zope.interface.Interface):
    password = zope.schema.Password(title=u"Passwort")
    password2 = zope.schema.Password(title=u"Wiederholung des Passworts")

    @zope.interface.invariant
    def arePasswordsEqual(user):
        if user.password != user.password2:
            raise zope.interface.Invalid(
                u"""Das Passwort und die Wiederholung sind nicht gleich.""")
-- 
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! 
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer


More information about the Zope3-users mailing list