[Zope3-Users] Validation of schemas?

Adam Summers adam at 4js.com.au
Thu Nov 3 08:09:37 EST 2005


Hi again,

    Thanks for the help so far. Using schemas I can do the following:

    class IBuddy(interface.Interface):
        """Provides access to basic buddy information"""

        fullname = TextLine(title=_("Name"))
        email = TextLine(title=_("Email Address"), required=False)
        phone = TextLine(title=_("Phone Contact"), required=False)

        @interface.invariant
        def buddyInvariants(ob):
            if ob.fullname.startswith("Z"):
                raise zope.interface.Invalid("Names cannot start with Z")
            if ob.email is None and ob.phone is None:
                raise zope.interface.Invalid("Email and Phone cannot 
both be None")

and:

    class Buddy(persistent.Persistent):
        """Buddy Information"""

        zope.interface.implements(IBuddy)

        def __init__(self,fullname=None, email=None, phone=None):
                self.fullname = fullname
                self.email = email
                self.phone = phone
                IBuddy.validateInvariants(self)

This will do the validation on add. But how do I get the schema  to be 
validated on an
update to it?

One way I can see is to have update method with named parameters (which 
does the validation at the end of the method), but then (I think) I lose 
the  ability to have a <browser:editform> directive to generate the html 
form based on the schema for free.

Any suggestions?

Thanks in advance,
Adam

gnotari at linkgroup.it wrote:

>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
>
>_______________________________________________
>Zope3-users mailing list
>Zope3-users at zope.org
>http://mail.zope.org/mailman/listinfo/zope3-users
>  
>




More information about the Zope3-users mailing list