You are viewing revision #2 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
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.
Update! ¶
Replaced the $.yii.fix expression everywhere to yiiFix in the tutorial, so the dependency from jquery.yii.js has been removed.
Usage ¶
1) Register the snippet into the HEAD.
2) Configure your CActiveForm instance like this:
$this->beginWidget('CActiveForm', array(
...
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'afterValidate'=>'js:yiiFix.ajaxSubmit.afterValidate'
)
...
);
3) Configure your CHtml::ajaxSubmitButton instance like this:
echo CHtml::ajaxSubmitButton('Whateverlabel', 'whateverurl', array(
...
'beforeSend'=>'js:yiiFix.ajaxSubmit.beforeSend("#YOUR_FORM_ID")'
...
);
The snippet ¶
;yiiFix = {
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.