How to check activerecord validation before submit?

Hi everyone, I’ve set the validation for the fields in the model and when i submit the form by submitButton,the validation checks well and there is no problem.
i prevent the submission on the click of the submitButton and i do some UI and then submit the form manually by ajax.
What should i do to make the validation of the form happen before doing the UI, and then i submit.?

You do not have to prevent the submission of the form by clicking the button. Instead, you can hook up your code to the afterValidate event triggering as show below:

$('body').on('afterValidate', '#my-form-id', function () {
   // ... your UI code goes here
});

More info about activeForm JS events and methods here

1 Like

thank you kubove.
i researched what you told, and also There are good information here ,
In fact what should i do when i want to change a simple confirm alert like below :point_down:with a sweet alert?

$(’#contact-form’).on(‘beforeSubmit’, function (e) {
if (!confirm(“Everything is correct. Submit?”)) {
return false;
}
return true;
});

Eventually, I found how to validate form separately in the client-side by jQuery !
and resolved my issue .
this is what i used

$(’#FormID’).yiiActiveForm(‘validate’,true);

this just validate the attributes. :ok_hand: