[Zope3-Users] Validation of schemas?

baiju m baiju.m.mail at gmail.com
Mon Oct 31 00:12:43 EST 2005


On 10/31/05, Stephan Richter <srichter at cosmos.phy.tufts.edu> wrote:
> On Sunday 30 October 2005 04:45, Adam Summers wrote:
> > How do I implement a validation rule that says either email or phone
> > have to exist, but not necissarily both.
>
> You do that using invariants. Look at zope.interface/README.txt for details.


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)


Regards,
Baiju M


More information about the Zope3-users mailing list