A simple way to get Yii client-side form validation run when submitting by ajax

Comparing #1 with #2

Revision #2 was created by nlac nlac on Sep 14, 2014, 5:27:28 PM.

Replaced the $.yii.fix expression everywhere to yiiFix in the tutorial, so the dependency from jquery.yii.js has been removed.

Content

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
pdate!
 
 
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();
[...]