Revision #4 has been created by mrwallace on Mar 2, 2011, 1:38:19 PM with the memo:
Initial spell/grammar check and removal of duplicity by changing the Class model to Classroom.
Introduction
------------------
In this tutorial we will learn how to realize a create interface using a dialog.
[Here](http://www.yiiframework.com/wiki/72/cjuidialog-and-ajaxsubmitbutton "other tutorial")
there is a similar tutorial that uses
aAjax link
forto achi
eve the goal, but
here we will use a simpl
ier and more effective approach: the event onSubmit of the form.
Scenario
------------------
Let's im
magine
that we have a class
room with many students. If the user fill
s in the form of the stu
ndent and there is no
t the class
, he hasroom made, he will have to create a class
room first
, so loosing the input and lose the already inserted
input.
We want to allow the user to
insertcreate the class
inroom from the form of the student
s, without changing page
s.
Preparation of the form
------------------
First of all we need a form for
instertcreating the class
room. We can generate a Crud for class
room with gii and adapt the code to our needs.
Once we are satisf
acted ofied with our form
thaand it works with
the usual submit, we can use it in a dialog.
Enhance the action create
------------------
We need
now to enhance the action create of the class
room controller.
Let's change i
nt this way:[...]
public function actionCreate()
{
$model=new Classsroom;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Class
sroom']))
{
$model->attributes=$_POST['Class
sroom'];
if($model->save())
{[...]
echo CJSON::encode(array(
'status'=>'success',
'div'=>"Classroom successfully added"
));
exit;[...]
------------------
Now the back-end is done, let's write the dialog itself.
In the form of the student somew
here we add this code:[...]
```php
<?php echo CHtml::link('Create classroom', "", // the link for open the dialog
array(
'style'=>'cursor: pointer; text-decoration: underline;',
'onclick'=>"{addClass
room(); $('#dialogClass
room').dialog('open');}"));?>
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
'id'=>'dialogClass
room',
'options'=>array(
'title'=>'Create class
room',
'autoOpen'=>false,
'modal'=>true,[...]
<script type="text/javascript">
// here is the magic
function addClassroom()
{
<?php echo CHtml::ajax(array(
'url'=>array('class
sroom/create'),
'data'=> "js:$(this).serialize()",
'type'=>'post',[...]
if (data.status == 'failure')
{
$('#dialogClassroom div.divForForm').html(data.div);
// Here is the trick: on submit-> once again this function!
$('#dialogClass
room div.divForForm form').submit(addClass
room);
}
else
{
$('#dialogClass
room div.divForForm').html(data.div);
setTimeout(\"$('#dialogClass
room').dialog('close') \",3000);
}[...]
- 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 aut
homatically in a d
dlrop down list.
Summary[...]
- Prepare the usual creation with gii generated code
- Change the action create for answer to aAjax requests
- Place the link/dialog/js wherever you want.
This methodology is very co
nmfortable because it changes anything in the code of the _form, so any even
ut
ually added field in class
room will be available in both standard and dialog insert.