With Ajax form validation, if CSRF token is rotten (for example), server returns a 400 error, which is not processed.
A very small addition in file framework\web\js\source\jquery.yiiactiveform.js will use callback function to handle errors.
$.fn.yiiactiveform.validate = function(form, successCallback, errorCallback)
- Function already has Callback, so we only need to use it, and add one more option in the settings.
- Add a third variable (in two places):
$.fn.yiiactiveform.validate($form, function(data) {
var hasError=false;
$.each(settings.attributes, function(){
if (this.status == 2 || this.status == 3) {
hasError = $.fn.yiiactiveform.updateInput(this, data, $form) || hasError;
}
});
if(attribute.afterValidateAttribute!=undefined) {
attribute.afterValidateAttribute($form,attribute,data,hasError);
}
}, function(){ //errror callback
if(settings.errorValidate==undefined || settings.errorValidate()) {
return true;
}
}
);
- add config variable(in two places):
$.fn.yiiactiveform.defaults = {
...
errorValidate: undefined, // function() : boolean
...
and
settings.attributes[i] = $.extend({
...
errorValidate : settings.errorValidate,
...
How to use: simple add option in the ActiveForm with your callback function:
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'register',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'errorValidate'=>'js:function(){alert(\'SERVER ERROR\');}',
),
)); ?>
It works
jquery.yiiactiveform.js with this changes attached:
3664