I am using ActiveForm with the afterValidate()
event, however 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. Here is my code:
PHP:
<?php $form = ActiveForm::begin([
'id' => 'register-form',
'options' => [
'class' => 'validate-form',
],
'enableClientValidation' => true,
'enableAjaxValidation' => false,
'validateOnSubmit' => true,
'validateOnBlur' => true,
'validateOnChange' => true,
]); ?>
JS:
// JS afterValidate function
$('.validate-form').on('afterValidate', function (event, messages, errorAttributes) {
$('.error-popup').show();
return false;
});
HTML:
<div class="error-popup">Please review the errors in the form below.</div>
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 any validation errors 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.