On 10/31/05, Stephan Richter <[EMAIL PROTECTED]> 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"[EMAIL PROTECTED]"
  IBuddy.validateInvariants(buddy)


Regards,
Baiju M
_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to