preSubmit not cancelling the submit when returning false

preSubmit not cancelling the submit when returning false

B.ReddB.Redd Posts: 3Questions: 0Answers: 0

We've discovered that the submit event is not cancelling with the preSubmit event when the function returns false. Please see the example. Has anyone else run into or found a solution for this?:

const clientSideValidation = (editor, columnFields) => {
        const handlePreSubmit = (e, data, action) => {
            return false;
        };
        editor.on("preSubmit", handlePreSubmit);
    };

export default clientSideValidation;

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    I haven't been able to reproduce this issue I'm afraid. If you go to this example and pop open the console and enter:

    editor.on('preSubmit', function () {
      return false;
    });
    

    it will prevent all submissions from Editor.

    Can you give me a link to a test page showing the issue please?

    Regards,
    Allan

  • B.ReddB.Redd Posts: 3Questions: 0Answers: 0
    edited June 2023

    Hey, I fixed it! It was a simple fix, and I think I understand the reason for the error, now, though I would be interested in your feedback to confirm.

    We have 2 separate preSubmit calls--essentially:
    •"editor.on("preSubmit", clientSideValidation);" followed by
    •"editor.on("preSubmit", dataToDRFStructure);" calling 2 separate functions.
    The first one is for validation and the second for converting the data to Django Rest Framework format.

    The fix was to simply switch the order of the calls. I suspect that the validation was having the preSubmit return false and then the data conversion was having it return true, overriding the first call, and that's why the form submission wasn't canceled.

    Should we only have one preSubmit call as a best practice?

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Ah. Actually, I think Editor should halt the submission if any of the preSubmit event handlers returns false. The idea of event handlers is that you should be able to have as many as you want.

    Allan

Sign In or Register to comment.