Creating 2 Save Actions In The Same Controller?

Hi all,

I am building a data entry application where one particular area is quite large and there is a lot of information to enter.

Due to the way in which the staff enter the information it has become a requirement that they need have 2 save buttons on the CRUD create & update pages, one for ‘Save’ and one for ‘Save and close’. The save button will create/update the record and keep the user on the same page and the other will create/update the record but then re-direct them back to the index page where I have a Grid View of all records.

My action method in the controller currently looks like this:


public function actionUpdate($id)

{

		$model=$this->loadModel($id);

		

		$this->pageTitle = "Property #". $id;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Properties']))

		{

			$model->attributes=$_POST['Properties'];

			if($model->save())

				$this->redirect(array('update','id'=>$model->pr_id));

		}


		$this->render('update',array(

			'model'=>$model,

		));

}

As you can see above when a user updates a record it currently keeps them on the same page so that they are ready to amend more fields.

I am guessing that I need to do something around the $model->save() property but I can’t work out how to separate the save actions and put them into a button.

Has anyone managed to do this and can anyone be kind enough to point me in the correct direction?

Many thanks

Did you try to set js redirect on success in the submit button ? I didnt try it but maybe it will help.

In case of submit buttons give them the same name but different values so that you can check value with something like




Yii::app()->request->getPost('button_name'); // should be equivalent to $_POST['your-form-name']['button_name'] I think

// if you have problems check the posted data in your controller with

//CVarDumper::dump($_POST);



Dependent on this button value you can stay on same site or redirect to index page.

Thanks for the suggestions guys.

I actually worked out another way of doing this. I don’t know whether it is properly Yii-correct but it seems to work.

Buttons in the view…


<?php echo CHtml::submitButton('Save', array('name' => 'save', 'class' => 'btn btn-medium btn-primary')); ?>


<?php echo CHtml::submitButton('Save & Close', array('name' => 'saveclose', 'class' => 'btn btn-medium btn-secondary')); ?> 

The Controller…


public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Properties']))

		{

			$model->attributes=$_POST['Properties'];

			if($model->save())

		

		 	if(isset($_POST['save']))

        	{

        		$this->redirect(array('update','id'=>$model->pr_id));

        	}

		

      		if(isset($_POST['saveclose']))

        	{

        		$this->redirect(array('index'));

        	}


		}


		$this->render('update',array(

			'model'=>$model,

		));

}

I hope this helps anyone else who might want to do something like this.

You got the idea how to do it, nice

It is another - more html clean/valid way I think - way of doing it. I could imagine that in W3C specification buttons with same names are not allowed.