formdialog Form dialog allow you to transform each create link in a ajax/dialog link.

  1. Requirements
  2. Usage

This extension provide an easy way to implement this wiki.

Requirements

Yii 1.1 or above

Usage

Extact the folder in the extensions folder.

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(isset($_POST['Classroom']))
		{
			$model->attributes=$_POST['Classroom'];
			if($model->save())
			{
				if (Yii::app()->request->isAjaxRequest)
				{
					exit CJSON::encode(array(
						'status'=>'success', 
						'message'=>"Classroom successfully added"
						));
									
				}
				else
					$this->redirect(array('view','id'=>$model->id));
			}
		}
		
		if (Yii::app()->request->isAjaxRequest)
		{
			exit CJSON::encode(array(
				'status'=>'failure', 
				'form'=>$this->renderPartial('_form', array('model'=>$model), true)));
							
		}
		else
			$this->render('create',array('model'=>$model,));
	}

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

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

Change the link

Choose the link you want to make dialog and change from this:

<?php echo CHtml::link('Create', array('create')) ?>

To this:

<?php echo CHtml::link('Create', array('create'), array('class'=>'create')) ?>
<?php $this->widget('application.extensions.formDialog.FormDialog', array('link'=>'a.create', 'options'=>array('onSuccess'=>'js:function(data, e){alert(data.message)}')))?>

In this way there at the on click instead of redirecting on the page create the create form will be open in a dialog, will be resubmitted all times needed for correct eventually input errors, and at success the message will be displayed.

You can use the option 'onSuccess' for do more interesting stuffs, like redirecting on the view page of the newly created item or reloading the CGridView if you are in the action index.

5 0
12 followers
627 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: User Interface
Tags: create, dialog
Developed by: zaccaria
Created on: Oct 2, 2012
Last updated: 12 years ago

Downloads

show all

Related Extensions