#StackBounty: #php #yii2 Yii2 – ActiveForm and afterValidate events

Bounty: 250

With the following ActiveForm config:

<?php $form = ActiveForm::begin([
    'id' => 'register-form',
    'options' => [
        'class' => 'validate-form',
    ],
    'enableClientValidation' => true,
    'enableAjaxValidation' => false,
    'validateOnSubmit' => true,
    'validateOnBlur' => true,
    'validateOnChange' => true,
]); ?>

// JS afterValidate function
$('.validate-form').on('afterValidate', function (event, messages, errorAttributes) {
    $('.error-popup').show();

    return false;
});

Currently the afterValidate() gets triggered on any validation event. How can I differentiate between the events validateOnSubmit, validateOnBlur and validateOnChange?

Basically, I only want my afterValidate() function to get triggered on submit.

EDIT: To give you a bit more details of what I am trying to achieve, as you can see in my afterValidate() function above, an error popup is shown if there are validation errors. However I only want this to error popup to appear if there are validation when the user submits the form.

If there is a validation error on blur/change then the normal inline validation should occur without any error popup.

From my understanding, beforeSubmit event is only triggered when validation has passed.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.