Revision #2 has been created by nlac on Sep 14, 2014, 5:27:28 PM with the memo:
Replaced the $.yii.fix expression everywhere to yiiFix in the tutorial, so the dependency from jquery.yii.js has been removed.
« previous (#1)
Changes
Title
unchanged
A simple way to get Yii client-side form validation run when submitting by ajax
Category
unchanged
Tutorials
Yii version
unchanged
Tags
unchanged
validation, ajax form submiting, CActiveForm, Forms
Content
changed
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.
### U
sage
1) Register the snippet into the HEAD. Be sure that it is linked after jquery.yii.js is linkedpdate!
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:[...]
'clientOptions'=>array(
'validateOnSubmit'=>true,
'afterValidate'=>'js:$.yii
.fFix.ajaxSubmit.afterValidate'
)
...[...]
echo CHtml::ajaxSubmitButton('Whateverlabel', 'whateverurl', array(
...
'beforeSend'=>'js:$.yii
.fFix.ajaxSubmit.beforeSend("#YOUR_FORM_ID")'
...
);[...]
```php
;$.yii
.fFix = {
ajaxSubmit : {
beforeSend : function(form) {
return function(xhr,opt) {
form = $(form);
$._data(form[0], "events").submit[0].handler();[...]