Image Uploading Error (When Empty)

hi i used Image Uploading In yii

i added successfully but when submit form with out upload Image i have face Error .i want to add Error MSG when Image is Empty How do this

My Controll

public function actionCreate()


{


	$model=new testing1;





	// Uncomment the following line if AJAX validation is needed


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





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


	{


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


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


            // random number + file name


            $fileName = "{$rnd}-{$uploadedFile}"; 


            // get image with Number


            $model->image = $fileName;


		if($model->save())


		$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$fileName);//save image


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


	}





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


		'model'=>$model,


	));


}

My View

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'category-places-form',


	'enableAjaxValidation'=&gt;false,


'htmlOptions' =&gt; array(


			'enctype' =&gt; 'multipart/form-data',),

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'name'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'name',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'name'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'image'); ?&gt;


	&lt;?php echo CHtml::activeFileField(&#036;model, 'image'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'image'); ?&gt;


&lt;/div&gt;

<div class="row buttons">

	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


&lt;/div&gt;

My Model

public function rules()

{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('name, type_id', 'required'),


		array('image', 'file','types'=&gt;'jpg, png',  'maxSize'=&gt;1024 * 1024 * 1, 'tooLarge'=&gt;'File has to be smaller than 1MB' , 'allowEmpty'=&gt;true, 'on'=&gt;array('insert','update')),


		array('type_id, Active', 'numerical', 'integerOnly'=&gt;true),


		array('name, image', 'length', 'max'=&gt;255),


		array('metadate', 'safe'),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('id, name, image, type_id, metadate, Active', 'safe', 'on'=&gt;'search'),


		array('image', 'file','types'=&gt;'jpg, gif, png', 'allowEmpty'=&gt;true, 'on'=&gt;'update'),


		array(' image', 'length', 'max'=&gt;255, 'on'=&gt;'insert,update'),


	);


}

How Add Error Message when Upload Image is Empty

public function rules()

{

return array(

array(‘image’,‘imgVail’),

)

}

public function imgVail()

{

if($this->img == ‘’)

 &#036;this-&gt;addError('img','Img can not blank');

}

Hope it will be userful

Thank you but i have to face this after add that code

Error 404

The requested page does not exist.

Hi,

You just add the validation check before ->save() method.




public function actionCreate()

{

$model=new testing1;


// Uncomment the following line if AJAX validation is needed

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


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

{

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

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

	// random number + file name

	$fileName = "{$rnd}-{$uploadedFile}"; 

	// get image with Number

	$model->image = $fileName;

	

	//Add the validation check here

	if($model->validate()){

		if($model->save())

			$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$fileName);//save image

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

		}

	}


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

		'model'=>$model,

		));

}



at your model add the image field to required




array('image,name, type_id', 'required'),



Now everything will work fine.

Cheers !