I am having some issues with saving a form. I have a form that lives inside a CJuiTab and an ajaxSubmit button that serializes the data to my controller, where the form is processed and saved. The problem is that the form is saved every time I focus out of a field, resulting in multiple new records. I want to execute my save method only when the ajaxSubmitButton is clicked, but my $_POST values do no include the ajaxSubmitButton id or name attributes to check against.
How can I make the controller detect when the user actually clicks the button so I can save only on click for the $_POST using the ajaxSubmitButton name or id?
View:
echo CHtml::ajaxSubmitButton(
$model->isNewRecord ? 'Save' : 'Update',
$this->createUrl('//HelpDesk/CaseEntry',array('case_number'=>$model->case_number)),
array(
'update'=>'#CaseForm',
'data'=>'js:$("#case_form").serialize()',
),
array(
'id'=>'save',
'name'=>'save',
));
Controller:
public function actionCaseEntry(){
$model = $this->getModel();
if(Yii::app()->getRequest()->getIsAjaxRequest()){
print_r($_POST); //shows only form fields, not ajaxSubmitButton
/*
So I can't do something like this:
if($_POST['HelpDesk']['save']){
//save here
}
*/
if(isset($_POST['HelpDesk'])){
$model->attributes = $_POST['HelpDesk'];
if(isset($_REQUEST['save'])){
Yii::app()->user->setFlash("success","save detected!");
}
else{
Yii::app()->user->setFlash("error","save not detected!");
}
//proceeding with save. (want this only when button is actually clicked):
if($model->validateCase()){
//do save/update:
if($model->save(false)){
$action = ($model->isNewRecord == true) ? "saved" : "updated";
Yii::app()->user->setFlash("success","Case #".$model->incident_id." ".$action."!");
}
else{
Yii::app()->user->setFlash("error","Case not saved!");
print_r($model->getErrors());
}
}
else{
Yii::app()->user->setFlash("error","Validation Error!");
print_r($model->getErrors());
}
}
}
$this->renderPartial('_case',array('model'=>$model),false,true);
}
Any ideas?