php - Yii2 - ActiveForm and afterValidate events

Solution:

Yii's active form JS save some info into yiiActiveForm data property. There is submitting property that you can use to determine if the form is in validation before form submission.

$('.validate-form').on('afterValidate', function (event, messages, errorAttributes) {
    let data = $('.validate-form').data('yiiActiveForm');
    //check if we are in submission process and if there are any errors
    if (data.submitting && errorAttributes.length > 0) {
        $('.error-popup').show();
    }
});

Answer

Solution:

You can approach this the following way:

$("#register-form").on("beforeSubmit", function(event){
    $(this).yiiActiveForm('validateAttribute', 'contactform-phone');
    $(this).yiiActiveForm('validateAttribute', 'contactform-email');
    if ($(this).find('.has-error').length) {
        ...
    }
    ...
    return false;
});

The submit button triggers beforeSubmit event which in turn triggers validation of whichever fields you need. If validation results in error, you can do your JS logic associated with displaying error messages.

Answer

Solution:

You should find has-error class after afterValidate and show popup try the following code

$('.validate-form').on('afterValidate', function (event, messages, errorAttributes) {
        if ($('.validate-form').find('.has-error').length) {
            $('.error-popup').show();
        }
  });

Source