Zine

open source content publishing system


Ticket #215 (new defect)

Opened 9 months ago

Last modified 9 months ago

"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

before-comment-created.patch (1.1 kB) - added by arteme 9 months ago.
Prososed patch.
before-comment-created-2.patch (2.3 kB) - added by arteme 9 months ago.
A better patch that allows a plugin to signal that the form is invalid without providing an error message.

Change History

Changed 9 months ago by arteme

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:

  • "before-comment-created" signal will not be emitted if some form fields already fail field validation,
  • event handlers will run until the first one emits the exception (is this true?).

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:

Consider a CAPTCHA implementation that would like to notify the user if the CAPTCHA validation failed regardless of other fields failing validation.

This means that the emit code must be placed in the NewCommentForm.create_if_valid() and must run independently of the self.validate() call.

Changed 9 months ago by arteme

Prososed patch.

Changed 9 months 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 9 months ago by arteme

A better patch that allows a plugin to signal that the form is invalid without providing an error message.

Changed 9 months 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.

Note: See TracTickets for help on using tickets.