[Zope3-Users] Validation of schemas?

gnotari at linkgroup.it gnotari at linkgroup.it
Mon Oct 31 04:02:06 EST 2005


zope3-users-bounces at zope.org wrote on 31/10/2005 06.12.43:


> Just added this to FAQ (http://zissue.berlios.de/z3/z3faq.html).
> I expects more contribution  in coming months.
>
>
> How to validate two or more fields simultaneously?
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Use `invariants` to control more fields.
>
> Look at zope.interface/README.txt for details.
>
> Let's consider this more specific question asked to list:
>
>   How do I implement a validation rule that says either email or phone
>   have to exist, but not necissarily both.
>
>   Using the buddy demo as an example, with the following interface::
>
>     class IBuddy(zope.interface.Interface):
>         """Provides access to basic buddy information"""
>
>         fullname = TextLine(title=_("Name"))
>         email = TextLine(title=_("Email Address"))
>         phone = TextLine(title=_("Phone Contact"))
>
> First we have to make a callable object, either a simple function or
> callable instance of a class::
>
>   def contacts_invariant(obj):
>       if not (obj.email or obj.phone):
>           raise Exception("At least one contact info is rquired")
>
> Now use `validateInvariants` method of the interface to validate::
>
>   buddy = Buddy()
>   buddy.email = u"user.id at some.address.com"
>   IBuddy.validateInvariants(buddy)

What I would want to be further explained is: When should I call
validateInvariants?
Of course I would call it when I'm creating or editing the object. Then,
frequently, just after an add or edit form.
More: can I then use auto generated forms (by using events after the form,
but how?), or I just have do define a custom form?
And then: what do I do if the validation falils? How do I return the form,
possibly with an error message?

I feel these are genuine FAQs, and I still don't have answers for many of
them. Working through them at a steday pace, though ;)
Thanks for all your help, really.

ciao
Guido



More information about the Zope3-users mailing list