This is pretty simple actually, here is my suggestion:
In your view file, where the initial form resides, make the form call like this:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
'focus'=>array($model,'title'),
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>false,//this needs to stay on false always.
'beforeValidate'=>"js:function(form){
return true;
}",
'afterValidate'=>"js:function(form, data, hasError){
if(hasError){
//do smth if there is an error.
}else{
// submit the data to your controller.
$.ajax({
url: $(form).attr('action'),
type:'POST',
data:$(form).serialize(),
dataType:'json',
success:function(obj){
if( obj.result === 'success' ){
$('#OtherModel_some_field1').val(obj.field_value1);
$('#OtherModel_some_field2').val(obj.field_value2);
$('.some_selector').dialog('open');
}
}
});
}
return false;
}"
),
));
?>
//your form fields here
<?php $this->endWidget(); ?>
The above code is pretty self explanatory, so let’s move on.
In this page (where is the form) use the CJui Dialog widget to create a dialog and in the dialog a form.
The dialog is hidden by default, so this form won’t be visible until the ajax submission is completed and the dialog opening is triggered.
Now, the controller code that will handle the first form(not the one from dialog) :
public function actionCreate()
{
$model=new User('save');
$this->performAjaxValidation($model);
$this->saveFormData($model);
$this->render('create',array('model'=>$model));
}
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
protected function saveFormData($model)
{
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];//clean this array.
if($model->save())
{
//this array will be used in the success function of the ajax call.
$return=array(
'result'=>'success',
'field_value1'=>$model->some_value1,
'field_value2'=>$model->some_value2,
);
echo CJSON::encode($return);
Yii::app()->end();
}
}
}
As you see , with the above code, we validate the first form and we return the data we need to use in the ajax success function .
With this step, we are done with the first form, so remember, when this form is submitted successfully and saved, it will return some data that we will use in the next form, but also, it will open the dialog containing the second form.
For the form within the dialog, the steps are easier.
Create the form just like we created the initial one, same thing for the controller, the only difference is that, in the success function of the ajax call, you will do other action, say, refresh the page or anything else.
The above data is some dummy data from one of my projects, but you get the point, right ?
Anyway, if you have questions, let me know