How To Actually Render A Success View?

So I have an upload form. It uploads an image, but I want it to redirect to a success view with some additional information.

I had a function in my FileController called actionSuccess, but I don’t really know what to put in this function in order to check if you came from the file upload page with an upload that was valid. This page should also be unavailable to view without uploading anything.

I’m not even sure whether or not I should have the function actionSuccess.

Hi

you can check something looklike this


if(isset($_POST['Coupon']['image_url']) && empty($_POST['Coupon']['image_url'])){

				if((isset($_FILES['Coupon']['name']['image'])) && empty($_FILES['Coupon']['name']['image'])){

					if($image == ''){

						Yii::app()->user->setFlash('error', Yii::t("messages","Please Upload a One of Them Images"));

						$this->render('create',array('model'=>$model,));

						Yii::app()->end();

					}

				}else{

					if($_FILES['Coupon']['name']['image'] != ''){

						$model->image = $_FILES['Coupon']['name']['image'];

						$model->image = CUploadedFile::getInstance($model, 'image');

						if(isset($model->image) && !empty($model->image)){

							$model->image->saveAs($path.'/upload/coupon/'.$model->image);

						}

						if($_POST['Coupon']['image_url'] == '')

						{

							$image_path=$url.'/upload/coupon/'.$model->image;

							$model->image_url=$image_path;

						}

					}

				}

			}

Note : u can display the flash message so how to diaplay in flash messge

put the below code iun your view file disaply flash message


<?php

	foreach(Yii::app()->user->getFlashes() as $key => $message) {

        echo '<div class="flash-' . $key . '">' . $message . "</div>\n";

    }

    ?>

Hi

you can check it by this code


$model->yourfile = CUploadedFile::getInstance($model, 'yourfile'); //if uploaded exist


if ($model->validate()) {

 ...

}

First:




class SomeController {

   public function actionSomeUploadHandler() {

  	// HANDLE YOUR UPLOAD HERE THEN IN IF

  	if($upload_success) {

    	Yii::app()->getUser()->setFlash('upload-done', TRUE);

    	$this->redirect('success');

  	} else {

    	// DO FOR FAILED UPLOAD

    	// OR

    	// SET FLASH AGAIN TO HANDLE/RENDER ANOTHER VIEW

  	}

   }


   public function actionSuccess() {

  	if(Yii::app()->request->urlReferrer === $this->createUrl('SomeUploadHandler') && Yii::app()->user->hasFlash('upload-done'))

    	$this->render('RENDER-SUCCESS-VIEW-HERE');

    	// <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />

  	} else {

    	$this->render('CAN-RENDER-FAILED-VIEW-TOO');

    	// <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' />

  	}

   }

}



Oh my, this is it, this is exactly what I was looking for. Didn’t realize you could actually check what link you were coming from and set a flash.

To expand on this…

The line


if (Yii::app()->request->urlReferrer === $this->createUrl('SomeUploadHandler'))

will return false, because the way it outputs both the variables is different.


Yii::app()->request->urlReferrer

will get the URL which you came from, eg: www.example.com/index.php/SomeUploadHandler/1, while the createUrl function from the controller will output something similar to /SomeUploadHandler/1.

I solved this by simply using PHP’s substr function.

This way isn’t 100 % reusable, sadly, but it works for my one-man project and I’ll change if it’s needed to maybe a regex function or similar.

Thanks for your help, guys! Great community.