array(
'clientOptions'=>array(
'validateOnSubmit'=>true, // Required to perform AJAX validation on form submit
'afterValidate'=>'js:mySubmitFormFunction', // Your JS function to submit form
),
),Go ahead and setup your Javascript function
function mySubmitFormFunction(form, data, hasError){
if (!hasError){
// No errors! Do your post and stuff
// FYI, this will NOT set $_POST['ajax']...
$.post(form.attr('action'), form.serialize(), function(res){
// Do stuff with your response data!
if (res.result)
console.log(res.data);
});
}
// Always return false so that Yii will never do a traditional form submit
return false;
}You really don't need to do anything special on the controller side with the exception that if you know you are going to processing an AJAX request (ala Yii::app()->request->isAjaxRequest and no $_POST['ajax']), you should be returning back a JSON object.
For example
// Validate ok! Saving your data from form okay!
// Send a response back!
header('Content-type: application/json');
echo json_encode('result'=>true, 'data'=>$modelDataOrSomeJunkToGiveBackToBrowser); // Use CJSON::encode() instead of json_encode() if you are encoding a Yii model
Yii::app()->end(); // Properly end the appEdit: I should clarify, it is true that your actual Javascript function to submit the form won't create a $_POST['ajax'] by default, but Yii doing the AJAX validation will (which already happened in the last AJAX request prior to calling your Javascript submit form function). So you can use the same logic in your controller for the AJAX validation as if you set CActiveForm.enableAjaxValidation property to true.