Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 client side form validation

I have a Yii2 ActiveFrom.

Replaced the Submit button with

    <button class="btn <?= $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary' ?>"
            onclick="submitit();return false;">
        <?= $model->isNewRecord ? 'Create' : 'Update' ?>
    </button>

because i need to show a modal overlay during the time the page is processing in the background. (Please wait...).

$('#rebates-form').yiiActiveForm('validate')

always returns undefined.

Even if i call

$("#rebates-form").data('yiiActiveForm')

after 'validate' the "validated" element is always false, even if the form don't has validation errors.

$('#rebates-form').yiiActiveForm('submitForm')

always return false, even if form got submitted.

How can i trigger my please wait dialog

$('#pleaseWaitDialog').modal('show');

to show only in the event of sumbiting the from without validation errors?

like image 691
Andreas Hinderberger Avatar asked Dec 04 '25 02:12

Andreas Hinderberger


1 Answers

Following solution is working:

$(document).ready(function () {
    $("#rebates-form").on("beforeSubmit", function (event, messages) {
        $('#pleaseWaitDialog').modal('show');
        return true;
    });
});

function submitit(element) {
    $('#rebates-form').yiiActiveForm('submitForm')
}

Where the extra button witht the submitit function call can also be left out, since the event "beforeSubmit" will trigger anyways.

This event will only be triggered if the form validation was successfull.

(see comment at https://github.com/yiisoft/yii2/blob/master/framework/assets/yii.activeForm.js)

like image 165
Andreas Hinderberger Avatar answered Dec 05 '25 17:12

Andreas Hinderberger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!