postSubmit event inconsistency

postSubmit event inconsistency

rduncecbrduncecb Posts: 125Questions: 2Answers: 28

The postSubmit documentation specifies the parameters of an event handler should be( e, json, data, action ).

It appears the event is triggered for success and failed ajax responses by Editor.prototype._submitSuccess and Editor.prototype._submitError respectively.

When fired from _submitError the parameters provided to the function are (xhr, err, thrown, submitParams), inconsistent with the documentation. It's also not possible to intercept the response and have it processed as if it was a successful submission in this case. As a consequence, if the ajax call responds with a 4xx code for validation errors (as the rest api does that i am working with) it can't be intercepted/tweaked for automatic processing by Editor as it would for the same response with a 20x code.

I was able to work around this by implementing my own processing function bound to the submitError event but it means implementing code to add errors to the Editor dialog myself. So that I don't have to re-implement code in the _submitSuccess function to add errors it would be nice if this functionality was refactored into a function I could call on an Editor instance to process a valid Editor error response JSON object ;)

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Very good point - thanks for flagging this up. This code was changed for 1.6 and it appears I neglected to update the documentation correctly.

    I've added it to my bug tracker and will have it fixed for the next release of Editor. I'll post back here when done and with any updates.

    Regards,
    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I've just committed a change that will be in Editoir 1.6.3 so that postSubmit will pass the same parameters for both the error and success conditions (per the documentation).

    However, if the server returns anything other than valid JSON, the JSON parameter passed into the event handler will be null and you don't have the option of modifying that (I couldn't find a good way to do that without breaking backwards compatibility).

    So what you have to do, if this is the situation for you, is to use your own complete function that will simply set the responseJSON parameter of the XHR object to be an empty object - e.g.

    complete: function ( xhr ) {
       xhr.responseJSON = {};
    }
    

    Then Editor won't fall into its error state.

    I've updated the documentation to say:

    Post-submit event for the form, fired immediately after the data has been loaded by the Ajax call, allowing modification or any other interception of the data returned form the server.

    This event is called for both the success and error conditions - i.e. regardless of what the server returned, this event will be triggered.

    If you wish to manipulate the JSON that Editor will use as the data returned by the server, please modify the first argument (json) that is passed into the event handler, rather than trying to modify the responseJSON or responseText parameters of the XHR object, which Editor will abstract out the difference between.

    Important: If the server returns any data that is not valid JSON, Editor will fall into its error state and null will be passed in as the json parameter. If you want to manipulate this error response into a successful response, you need to use a custom complete function on the ajax object that will mutate the responseJSON/responseText (depending on your version of jQuery) parameters of the XHR object to contain valid JSON.

    I'm going to make the 1.6.3 release today, and 1.6.3+ will be required for this to work correctly.

    Thanks again for flagging this up!

    Allan

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    edited May 2017

    Excellent! Thanks for replying when you had a fix. I'll give 1.6.3 a whirl and see about removing my workaround.

    I have no need for a complete function, I simply need to restructure the response into something suitable for Editor to consume and let it do its thing.

This discussion has been closed.