zopefoundation / z3c.form

An advanced form and widget framework for Zope 3
Other
8 stars 39 forks source link

WidgetActionExecutionError #42

Closed idgserpro closed 8 years ago

idgserpro commented 8 years ago

When I have an invariant that launches a WidgetActionExecutionError, this exception is not catch and is show in the log:

Traceback (innermost last):
  Module ZPublisher.Publish, line 138, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 48, in call_object
  Module plone.app.z3cform.inline_validation, line 29, in __call__
  Module z3c.form.form, line 148, in extractData
  Module z3c.form.field, line 324, in extract
  Module z3c.form.field, line 216, in validate
  Module z3c.form.validator, line 201, in validate
  Module z3c.form.validator, line 206, in validateObject
  Module zope.interface.interface, line 590, in validateInvariants
  Module brasil.gov.integracaocaldav.importagenda, line 38, in validatedatainicialdatafinal
WidgetActionExecutionError

This is because the method validateObject only catch exceptions of type zope.interface.Invalid and WidgetActionExecutionError doesn't inherit from zope.interface.Invalid.

icemac commented 8 years ago

This is the way invariants are designed, see

In an invariant simply do not raise an exception which does not inherit zope.interface.Invalid. BTW WidgetActionExecutionError is an exception internally used by z3c.form. I could not see a reason why you should use it in your validation code.

idgserpro commented 8 years ago

@icemac I want to validate a field that depends on another field. But I want the error message to be shown in that particular field and not on top of form.

My code is based on: http://docs.plone.org/develop/addons/schema-driven-forms/customising-form-behaviour/validation.html#validating-in-action-handlers

idgserpro commented 8 years ago

@icemac maybe I should raise an WidgetActionExecutionError in handler of button, as shown in the code indicated, instead raise in the invariant. Is this?

icemac commented 8 years ago

@idgserpro If you want to display the error message on a specific field you have to follow the approach shown on docs.plone.org validating in the action handler. I am currently not aware how this would be possible using interface invariants.

idgserpro commented 8 years ago

@icemac Thank you!

idgserpro commented 8 years ago

@icemac maybe this:

In an invariant simply do not raise an exception which does not inherit zope.interface.Invalid.

should be documented somewhere.