[Zope3-Users] z3c.form - how to raise/create an error in an action?

Hermann Himmelbauer dusty at qwer.tk
Mon Aug 13 15:00:21 EDT 2007


Am Montag, 13. August 2007 19:42 schrieb Stephan Richter:
> On Monday 13 August 2007 12:02, Hermann Himmelbauer wrote:
> > I have a case where an error may occur in the form-action method
> > (z3c.form, not formlib!), e.g. when data is not found in my database. I'd
> > then like to output this error, similar when a widget error occurs.
> > Moreover there are two simple cases:
> >
> > 1) Errors that are related to a widget (hence the error should be shown
> > along with the widget)
> > 2) Errors which are unrelated to the widget (like an Invariant).
>
> Hi Hermann,
>
> could you elaborate a little bit more? I think a step-by-step description
> of how *you* envision it to work would be great as well as a very specific
> use case of what you are trying to do.

Well, in my specific case, I have a search form for persons that queries (via 
zalchemy) a relational database and saves the result on the view object. Then 
I have a page template that displays this object.

My form also has an interface with some input fields, which are validated via 
the schema and I also have an invariant. In case there's an error, there is 
the error macro in the formui page template that displays these errors, which 
is fine. In my form there are three input fields after which can be searched:

1) A unique person ID (in case the user already knows it). This field has 
precedence.
2) A surename and name field (which may give multiple results)

Then I have a search button and a corresponding action. In this action, a 
DB-search is performed. There are two error cases:

1) The user enters an ID, which is invalid/unknown. In this 
case, "invalid/unknown ID" should be denoted for the widget and in the form 
status.
2) The user enters the name. In case there are no matching records, a "not 
found" error message should be displayed in the form status, which is not 
widget-related.

What I would favor is that the error-macros of the formui package can be 
reused, hence I need the same error objects in the view. Therefore I have to 
somehow set these errors. My first thought was to do it the following way 
(for the two cases):

1) Widget-specific error: Somehow set the error field, e.g. in the action:

self.widgets['myIDfield'].error="Invalid/unknown ID"
Another variant would be to raise a specific error, like this:

raise WidgetActionError(['myIDfield', 'invalid/unknown ID'])

2) Generic error: Raise an exception, similar like in invariants:

raise Invalid("No data was found")

> My initial reaction is: This really needs to be supported by the API. Once
> I have more details, I will either implement an API for this or give you a
> response on how to do it in your code.

Great! Thanks a lot for your quick reply!

Best Regards,
Hermann

-- 
x1 at aon.at
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7


More information about the Zope3-users mailing list