Difference between #5 and #4 of CJuiDialog to create new model

CJuiDialog for create new model
juidialog, user interface, AJAX

In this tutorial we will learn how to realize a create interface using a dialog.

"other tutorial") is a similar tutorial that uses Ajax link to achieve
the goal, but we will use a simpler and more effective approach: the event
onSubmit of the form.


Let's imagine we have a classroom with many students. If the user fills in the
form of the student and there is no classroom made, he will have to create a
classroom first and lose the already inserted input.

We want to allow the user to create the classroom from the form of the student,
without changing pages.

Preparation of the form

First of all we need a form for creating the classroom. We can generate a Crud
for classroom with gii and adapt the code to our needs.

Once we are satisfied with our form and it works with the usual submit, we can
use it in a dialog.

Enhance the action create

We need to enhance the action create of the classroom controller.

Let's change it this way:

	public function actionCreate()
		$model=new Classroom;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

				if (Yii::app()->request->isAjaxRequest)
					echo CJSON::encode(array(
						'div'=>"Classroom successfully added"
		if (Yii::app()->request->isAjaxRequest)
			echo CJSON::encode(array(
				'div'=>$this->renderPartial('_form', array('model'=>$model),

We add some small changes: in case of ajax request we write a json encoded

In this array we return a status (failure/success) and the whole form created
with renderPartial.

The dialog

Now the back-end is done, let's write the dialog itself.

In the form of the student somewhere we add this code:


<?php echo CHtml::link('Create classroom', "",  // the link for
open the dialog
		'style'=>'cursor: pointer; text-decoration: underline;',

$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
        'title'=>'Create classroom',
<div class="divForForm"></div>

<?php $this->endWidget();?>

<script type="text/javascript">
// here is the magic
function addClassroom()
	<?php echo CHtml::ajax(array(
			'data'=> "js:$(this).serialize()",
				if (data.status == 'failure')
					$('#dialogClassroom div.divForForm').html(data.div);
                          // Here is the trick: on submit-> once again this
					$('#dialogClassroom div.divForForm form').submit(addClassroom);
					$('#dialogClassroom div.divForForm').html(data.div);
					setTimeout(\"$('#dialogClassroom').dialog('close') \",3000);
			} ",
	return false; 


And that's all. In this code we have:

- A link for open the dialog
- the dialog itself, with a div inside that will be replaced with ajax
- the javascript function addClassroom().

This function fires an ajax request to the action create we prepared in the
previous step.

The returned form will be placed in the dialog (with eventually, all errors and
so on) in case of status failure, in case of status success in the example we
replace the div and we close the dialog after 3 seconds.

If you use this system in the form for student, you can return, for example, the
id of the newly inserted classroom and select it automatically in a drop down


ForTo make a long story short:

- Prepare the usual creation with gii generated code
- Change the action create for answer to Ajax requests
- Place the link/dialog/js wherever you want.

This methodology is very comfortable because it changes anything in the code of
the _form, so any eventually added field in classroom will be available in both
standard and dialog insert.