Yii Framework Forum: Server Error And Enableajaxvalidation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Server Error And Enableajaxvalidation CSRF validation Rate Topic: ***** 1 Votes

#1 User is offline   Yuri! 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 30-December 10

Posted 05 January 2013 - 07:11 AM

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.

1. 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;
		}
	}
);


2. 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:

Attached File  jquery.yiiactiveform.zip (3.57K)
Number of downloads: 5
2

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users