Re: Validation and Events

George Lee <[email protected]> Tue, 26 Sep 2006 00:51:46 +0000 (UTC)
Newsgroups gmane.comp.web.zope.plone.archetypes.devel
Message-ID <[email protected]>
Martin Aspeli <optilude@...> writes:

> def post_validate(self, errors):
>      ...
>      # Let other validators do their magic
>      notify(ArchetypesValidationEvent(self, errors))
> 
>      # Did they do anything?
>      if len(errors) > 0:
>          return False # we have an error
> 
> This is pseudo-code, obviously. But the point is, if notify() didn't 
> block until all event handlers were finished, then you couldn't check 
> the 'errors' dict like that!


I'm confused what you mean by "block" -- block meaning, "CALL ALL THE HANDLERS,
NOW"? What other event handlers would it be waiting for?

Also, where would the definition of this Event go?

Finally, looking at how Archetypes' validation works, there seems to be a
natural place for the event to fire. From the definition of
Products.Archetypes.BaseObject.BaseObject:

    def validate(self, REQUEST=None, errors=None, data=None, metadata=None):
        """Validates the form data from the request.
        """
        if errors is None:
            errors = {}
        self.pre_validate(REQUEST, errors)
        if errors:
            return errors
        self.Schema().validate(instance=self, REQUEST=REQUEST,
                               errors=errors, data=data, metadata=metadata)
        self.post_validate(REQUEST, errors)
        return errors

And from the archetypes skin folder, validate_base.vpy (validate_integrity.cpy
is almost identical):

 errors = {}
 errors = context.validate(REQUEST=context.REQUEST, errors=errors,
   data=1, metadata=0)

 if errors:
     return state.set(status='failure', errors=errors,
       portal_status_message='Please correct the indicated errors.')
 else:
    return state.set(portal_status_message='Changes saved.')

So all that is needed is to fire the event right before "return errors" above to
add to to the errors dictionary.


Peace,
George


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV