[Zope3-dev] _validate on schema fields gets called before services exist
Jim Fulton
jim@zope.com
Sun, 22 Dec 2002 13:09:23 -0500
Steve Alexander wrote:
> I'm trying to write a field that validates its contents by asking a
> service a question.
>
> However, I can't assume that the service exists and use getService in
> the _validate method, because when zcml is read in as zope3 is started
> up, for some reason, the field's _validate gets called. I guess this is
> because the Schema I'm using the field in gets executed by the python
> interpreter.
>
> Does _validate have to be called at this time?
>
> What is the best thing to do when writing a Field that wants to use a
> service to help it validate things?
Actually, your problem was caused by something else. You initialized
your field like this:
permission = PermissionField("The permission needed to use the component.")
but should have initialized it like this:
permission = PermissionField(
title=u"The permission needed to use the component.")
This is a common mistake. We really do eventually need to:
- Make it possible to initialize fields with positional doc strings, and
- catch and prohibit other positional args to catch this common mistake.
Jim
--
Jim Fulton mailto:jim@zope.com Python Powered!
CTO (888) 344-4332 http://www.python.org
Zope Corporation http://www.zope.com http://www.zope.org