beforeSubmit, beforeValidate [Solved]

I’m using this JS code from Yii2-Cookbook:




Html::submitButton(HTML::icon('ok') . ' Add', [

    'id' => 'add_submit',

    'method' => 'POST',

    'class' => 'btn btn-default btn-xs',

    'name' => 'add',

    'value' => $staffLink->id,

]) ?>


<script>

    $('#add_submit').on('beforeSubmit', function (e) {

        if (!confirm("Everything is correct. Submit?")) {

            return false;

        }

        return true;

       

    });

</script>

I’m just trying to get this to work before I write my own function in place of the alert. I want this function to run before validation. I have tried both “beforeSubmit” and “beforeValidate”, but neither one works. It goes straight to validation. What am I missing?

I tried this




$this->registerJs(<<<'EOD'

$('#contact-form').on('beforeValidate', function (e) {

    if (!confirm("Everything is correct. Submit?")) {

        return false;

    }

    return true;

});

EOD

)



Resulting code added to page




<script type="text/javascript">jQuery(document).ready(function () {

$('#contact-form').on('beforeValidate', function (e) {

    if (!confirm("Everything is correct. Submit?")) {

        return false;

    }

    return true;

});



It works. I guess I need to learn more about registering assets. Thanks for your help.