You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
A time ago i've met the issue that Yii doesn't run any client-side form validation when submitting the form by CHtml::ajaxSubmitButton. The small javascript below helps to fix it.
Usage ¶
1) Register the snippet into the HEAD. Be sure that it is linked after jquery.yii.js is linked.
2) Configure your CActiveForm instance like this:
$this->beginWidget('CActiveForm', array(
...
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'afterValidate'=>'js:$.yii.fix.ajaxSubmit.afterValidate'
)
...
);
3) Configure your CHtml::ajaxSubmitButton instance like this:
echo CHtml::ajaxSubmitButton('Whateverlabel', 'whateverurl', array(
...
'beforeSend'=>'js:$.yii.fix.ajaxSubmit.beforeSend("#YOUR_FORM_ID")'
...
);
The snippet ¶
;$.yii.fix = {
ajaxSubmit : {
beforeSend : function(form) {
return function(xhr,opt) {
form = $(form);
$._data(form[0], "events").submit[0].handler();
var he = form.data('hasError');
form.removeData('hasError');
return he===false;
}
},
afterValidate : function(form, data, hasError) {
$(form).data('hasError', hasError);
return true;
}
}
};
i think below link should be better way
http://www.yiiframework.com/forum/index.php/topic/37075-form-validation-with-ajaxsubmitbutton/
which one among the 3 there and why
The method i published aims to fix CHtml::ajaxSubmitButton to serve better what is it for, enabling real client-side validation. I'm not sure if any of the 3 methods on that link does really make it, however i like one of those too. But hey - anyone can decide what is better for his taste.
does not work
does not work. lack of documentation. add sample source code
Where does the snippet go?
I'm getting an error:
Uncaught TypeError: Cannot set property 'fix' of undefined
Where does the snippet go?
@jbowler
check the update, that will solve the problem
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.