Editor call external function from preSubmit

Editor call external function from preSubmit

rldean1rldean1 Posts: 141Questions: 66Answers: 1

I'm doing form validation in preSubmit(). I want to check two columns for a constraint violation, and I figured this is the best spot to do it. I'm using straight-up JS/jQuery and SQL (no PHP).

Can I stop preSubmit(), run my function, and then proceed with submission from that function?

editor.on('preSubmit', function (e, data, action) {

    if (action !== 'remove') {

        //client-side validation of the fields

    }

    //If any error was reported, cancel the submission so it can be corrected
    if (this.inError()) {
        return false;
    }


    //stop form from submitting
    return false;

    //call my special function
    someSpecialFunction(data)



});



function someSpecialFunction(x) {

    // SQL was called, this function processes the returned info

    // {jsonResponse} from server looks like:
    // {"error", "This is a general error"}

    // set the error:
    editor.error("This is a general error")
    editor.inError()

    /**
     * if there isn't an error, allow preSubmit to finish ???
     * something like...
     */

    if (!editor.inError()) {

        editor.submit() //???????
    }

}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Yes, this is possible, but you need to use a little "trickery" since the submit() call will always trigger the preSubmit event. The way to do it is to add a flag that will stop preSubmit's event handler from returning false - example:

    var allowSubmitFlag = false;
    
    editor.on( 'preSubmit', function ( ... ) {
      if ( allowSubmitFlag === false ) {
        doValidation();
        return false;
      }
    
      allowSubmitFlag = false; // for next time around
    } );
    
    function doValidation () {
      ...
    
      // All valid
      allowSubmitFlag = true;
      editor.submit();
    }
    

    Allan

This discussion has been closed.