How To Upload Image

plz help me …

      when i upload image i face some validation error "Image cannot be blank."

this is my controller code




  	public function actionCreate()

	{

		$model=new Registration;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

            	if($model->validate()){

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

			if($model->save())

                  $model->image->saveAs('images/user_image/'.$image);

            	Yii::app()->user->setFlash('Registration','Thank you for registration with us. We will respond to you as soon as possible.');

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

        }

		}


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

			'model'=>$model,

		));

	}


 




this is my model code





  	public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('reg_name, reg_username, reg_password, reg_email,reg_image', 'required'),

			array('reg_name, reg_username, reg_password, reg_email', 'length', 'max'=>100),

          	array('reg_email', 'email'),

                             

          

            array('reg_image', 'length', 'max' => 255, 'tooLong' => '{attribute} is too long (max {max} chars).', 'on' => 'upload'),

    array('reg_image', 'file', 'types' => 'jpg,jpeg,gif,png', 'allowEmpty'=>true,'maxSize' => 1024 * 1024 * 2, 'tooLarge' => 'Size should be less then 2MB !!!', 'on' => 'upload'),

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

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

			array('reg_id, reg_name, reg_username, reg_password, reg_email', 'safe', 'on'=>'search'),

		);

	}



this is my view code





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

	'id'=>'registration-form',

    'enableClientValidation'=>true,

    'htmlOptions' => array('enctype'=>'multipart/form-data'),

	// Please note: When you enable ajax validation, make sure the corresponding

	// controller action is handling ajax validation correctly.

	// There is a call to performAjaxValidation() commented in generated controller code.

	// See class documentation of CActiveForm for details on this.

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'reg_name'); ?>

		<?php echo $form->textField($model,'reg_name',array('size'=>60,'maxlength'=>100)); ?>

		<?php echo $form->error($model,'reg_name'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'reg_username'); ?>

		<?php echo $form->textField($model,'reg_username',array('size'=>60,'maxlength'=>100)); ?>

		<?php echo $form->error($model,'reg_username'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'reg_password'); ?>

		<?php echo $form->passwordField($model,'reg_password',array('size'=>60,'value'=>'','maxlength'=>100)); ?>

		<?php echo $form->error($model,'reg_password'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'reg_email'); ?>

		<?php echo $form->textField($model,'reg_email',array('size'=>60,'maxlength'=>100)); ?>

		<?php echo $form->error($model,'reg_email'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'reg_image'); ?>

		<?php echo $form->fileField($model,'reg_image'); ?>

		<?php echo $form->error($model,'reg_image'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Register' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->



you’ve got reg_image as required in rules but I doubt it goes in POST becouse you use fileField (reg_image is available under $_FILES), So there is no reg_image while you assign attributes. Thats why you’re getting this error. If ju need to ensure that file is sent set allowEmpty to false in file validation rule.




public function rules()

        {

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

                // will receive user inputs.

                return array(

                        array('reg_name, reg_username, reg_password, reg_email', 'required'),

                        array('reg_name, reg_username, reg_password, reg_email', 'length', 'max'=>100),

                array('reg_email', 'email'),

                             

          

            array('reg_image', 'length', 'max' => 255, 'tooLong' => '{attribute} is too long (max {max} chars).', 'on' => 'upload'),

    array('reg_image', 'file', 'types' => 'jpg,jpeg,gif,png', 'allowEmpty'=>FALSE,'maxSize' => 1024 * 1024 * 2, 'tooLarge' => 'Size should be less then 2MB !!!', 'on' => 'upload'),

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

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

                        array('reg_id, reg_name, reg_username, reg_password, reg_email', 'safe', 'on'=>'search'),

                );

        }



PS. It think it’s not a good forum to solve such problems

Hi,

Video:Yii2 Lesson - 17 Uploading Files to the server by Doing it Easy will be helpful for you in this case,

Thanks,

Vishwas

[color="#006400"]Moved

‘Feature Request’ is not the same as ‘Help Request’ …[/color]