Update Image

Hi again to everyone =), i have a new problem with uploading image, i can upload the image with create action, but my problem is when i want update, i wanna can update my db table without new image, but ever say me image cannot be blank, i dont have the image field required…

Model code:




/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

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

		// will receive user inputs.

		return array(

			

			//.......

                        array('dni, nom, cognoms, adresa, data_naiximent, telefon_1, correu_electronic, lloc_naiximent, poblacio', 'required'),

			array('foto', 'unsafe'),

			array('foto', 'file', 'types'=>'jpg, gif, png'),

			array('dni', 'ext.MyValidators.dniValidate'),

			//.......

		);

	}



Controller code (update)




public function actionUpdate($id)

	{

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


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			$name = 'images/personas/'.md5(time()).'_'.$this->normalizar_nombre($model->foto);

			$foto_ = $model->foto;			

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

			$model->foto = $name;

			if($model->save())

			{				

				$foto_->saveAs($name);				

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

			}

		}


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

			'model'=>$model,

		));

	}



I tried with this comment in this wiki http://www.yiiframework.com/wiki/2/ but it give me the same error

[b]Grettings and thanks,

s0mk3t[/b]

you need to use scenarios so that you say on create the image is required… but on update it can be empty…

here is my image rules… just adapt them for your case…




...

array('name', 'file', 'types'=>'pdf,txt','on'=>'insert'),

array('name', 'file', 'allowEmpty'=>true,'types'=>'pdf,txt','on'=>'update'),

...



It work good, a lot of thanks!!

Hello everyone, :D

I also have a problem with form and uploading image.

What i need to make:

form with approximately 10 fields connected with db, of course. Typical CRUD scenario.One of that fields is "fileField" type, so I can upload image to server and write image name to database, so I could use it later. Form must allow option that in certain cases I will not upload image.

I followed these instructions: How to upload a file using a model

Everything is fine when I include that image, but the form is misbehaving when I don’t upload picture, whether I create or update. Instead of saving informations to db, it just rejects all informations from fields and does not update informations in database.

Something like “Cancel” button ::) but there is only “Save” button and it did not save.

I’ve read all the articles on this forum and on the Internet that talk about similar problems but did not managed to find a solution.

Here is my code.

Model:

Controller:

and view (_form):

I really don’t know what is wrong in my code and this problem is really bothering me… :unsure:

Your validation do not pass… that’s why nothing is saved…

you need to allow empty field for the file upload - http://www.yiiframework.com/doc/api/1.1/CFileValidator#allowEmpty-detail

Thaaaaaaaaaank you very much!!!!

You saved my life :lol:

Or shuld I say, hvala puno! :wink:

great Work Dear You Made my day

very very Thanks.

Hello!

I have a problem with image name update.

If i update the article which has image, the image name will be blank in database.

this is my controller:


public function actionUpdate($id)

	{

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

		$_SESSION['KCFINDER']['disabled'] = false;

                $_SESSION['KCFINDER']['uploadURL'] = Yii::app()->baseUrl."/../images/"; // URL for the uploads folder

                $_SESSION['KCFINDER']['uploadDir'] = Yii::app()->basePath."/../../images/"; // path to the uploads folder

		// Uncomment the following line if AJAX validation is needed

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

                $image_new = $model->img;

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

		{

			//$model->attributes=$_POST['News'];

                        $_POST['News']['img'] = $model->img;

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

//                        $model->img = $_POST['News']['img'];

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

                        

			if($model->save()){

                                if(!empty($uploadedFile))  // check if uploaded file is set or not

                                {

                                        $uploadedFile->saveAs(Yii::app()->basePath.'/../../images/'.$model->img);

                                }

                                

                               // $model->isNewRecord;

                                

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

                        }

		}


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

			'model'=>$model,

		));

	}

And this is my model:


	public function rules()

	{

		return array(

			array('title, content, create_at, lang, active', 'required'),

			array('active', 'numerical', 'integerOnly'=>true),

			array('title, intro, img, meta_keys, meta_desc', 'length', 'max'=>255),

			array('lang', 'length', 'max'=>5),

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

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

			array('id, title, intro, img, content, create_at, lang, active, meta_keys, meta_desc', 'safe', 'on'=>'search'),

                        array('img', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),

                        array('title, img', 'length', 'max'=>255, 'on'=>'insert,update'),


		);

	}

It works in create action, however if i am update the article, $model->img will be NULL.

Pls help me i dont have idea.

guys i have a problem, when i update student_name & student_npm, i need to upload the same picture as well since i am using student_name and student_npm to name the picture. i cannot leave if blank, i tried if ($model->file != null) but fail

public function actionUpdate($id)

{


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





    if ($model->load(Yii::$app->request->post()) ) {





        $imageNpm = $model->student_npm;


        $imageName = $model->student_name;





        $model->file = UploadedFile::getInstance($model,'file');


        $model->file->saveAs( Yii::getAlias('@frontend/web/uploads/').$imageNpm.'_'.$imageName.'.'.$model->file->extension);


        $model->student_picture = $imageNpm.'_'.$imageName.'.'.$model->file->extension;


        











        $model->save();


        return $this->redirect(['view', 'id' => $model->student_id]);


    } else {


        return $this->render('update', [


            'model' => $model,


        ]);


    }


}