Ticket #215 (new defect)
"before-comment-created" is never emitted
| Reported by: | arteme | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | |
| Component: | admin | Version: | dev |
| Keywords: | Cc: | arteme@… |
Description
Quoting zine.views.show_entry() docstring,
Events emitted:
`before-comment-created`:
this event is sent with the form as event data. Can return
a list of error messages to prevent the user from posting
that comment.
But the mentioned signal is never emitted. As mentioned in the description, there should be some provision for the signal handler to signal that the form should return a list of errors to prevent the comment from being posted.
Attachments
Change History
comment:2 Changed 3 years ago by arteme
The attached patch implements the event emitting as described in comment #2. Any errors returned by the "before-comment-created" event handlers will be merged with the form validation errors.
The handler function adheres to the description of the signal:
def before_created_check(req):
if ...:
return [u'Error A', u'Error B']
return
Changed 3 years ago by arteme
-
attachment
before-comment-created-2.patch
added
A better patch that allows a plugin to signal that the form is invalid without providing an error message.
comment:3 Changed 3 years ago by arteme
- Cc arteme@… added
After a bit more thought on the subject a new scenario emerged:
Suppose a plug-in injects a custom field to the "new comment" form and uses the "before-comment-created" event to trigger the field validation. It is then desirable to indicate a validation error in such a way that to system-wide error message is given. Instead, the plug-in will set a field-specific error message to be displayed next to the field.
A new patch is provided above to add such functionality - returning 'True' from an event handler will indicate a field-specific validation error without a form-wide error message. The documentation has been updated accordingly.
The most logical place to add the event emit is the NewCommentForm.context_validate(), then the event handlers can raise ValidationError exceptions. This method, however, has the following consequences:
With custom form validation done in the "before-comment-created" signal handler it may be desirable to show errors to the user regardless if the fact that other form fields failed validation. This can be illustrated with the following example:
This means that the emit code must be placed in the NewCommentForm.create_if_valid() and must run independently of the self.validate() call.