[Zope3-Users] z3c.form: how to use invariant/validator?

Peter Piehler peter.piehler at pienet.de
Sun Aug 5 16:51:40 EDT 2007


Hello,

recently I started working with Zope3 and z3c.form.
The figures in the form should be checked, for instance that the first name and the surname are not identical.
Therefore I write an invariant-check:

class IAddress(zope.interface.Interface):
    """Address Interface"""

    firstname = zope.schema.TextLine(
        title=u'firstname',
        description=u'')
    surname = zope.schema.TextLine(
        title=u'name',
        description=u'')

    @zope.interface.invariant
    def areEqual( address ):
        if address.firstname == address.surname:
            raise zope.interface.Invalid(u"first name and surname are same")

I was expecting that the error would be shown in the form.
Unfortunately, zope canceled the  action:
 File "buildout-eggs/zope.app.pagetemplate-3.4.0b1dev_r75616-py2.4.egg/zope/app/pagetemplate/engine.py", line 68, in __call__
   request=request)
 File "buildout-eggs/zope.traversing-3.4.0a1-py2.4.egg/zope/traversing/adapters.py", line 164, in traversePathElement
   return traversable.traverse(nm, further_path)
  - __traceback_info__: (<zope.interface.exceptions.Invalid instance at 0x2aaab13cf7a0>, 'widget')
 File "buildout-eggs/zope.traversing-3.4.0a1-py2.4.egg/zope/traversing/adapters.py", line 49, in traverse
   return subject[name]
  - __traceback_info__: (<zope.interface.exceptions.Invalid instance at 0x2aaab13cf7a0>, 'widget', ['label'])
TypeError: tuple indices must be integers

With the pdb you can see that the method traverse (in adapters.py) is executed twice with name='widget' and name='label':




Second try with own validator:

class
AddressValidator(z3c.form.validator.InvariantsValidator):
    def validateObject(self, obj):
        import pdb; pdb.set_trace()
        errors = super(AddressValidator,
self).validateObject(obj)
        if obj.firstname == obj.surname:
            errors += (zope.interface.Invalid('Firstname and Surname can not be equals.'),)
        return errors

configure.zcml: <adapter factory=".interfaces.AddressValidator"/>

If I try with the same figure twice, then Zope cancels the action same as above.

What should I do in order to get the error message correctly shown in the form?

Thanks a lot,
Peter



More information about the Zope3-users mailing list