Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating Image in Yii2

Tags:

yii2

While updating image using Yii2 I'm facing a problem with the validation.Its always asking me to upload an image. But I don't want this. Always updating an image is not necessary.

I tried skipOnEmpty but its not working properly it cause effect while uploading a photo, which is also incorrect.

Please help!!

Model

public function rules()
    {
        return [
            [['carid', 'name'], 'required'],
            [['carid', 'coverphoto', 'status'], 'integer'],
            [['name'], 'string', 'max' => 200],
            [['imageFiles'], 'image','extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 4, 'minWidth' => 100, 'maxWidth' => 800, 'minHeight' => 100, 'maxHeight'=>600,'skipOnEmpty' => true],


        ];
    }

Controller

public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->photoid]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
like image 336
Sangita Pal Avatar asked Oct 03 '15 07:10

Sangita Pal


1 Answers

You should to use scenario for update.

Like as, Add on condition in model's rule for applying scenario .

 [['imageFiles'], 'image','extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 4, 'minWidth' => 100, 'maxWidth' => 800, 'minHeight' => 100, 'maxHeight'=>600,'skipOnEmpty' => true, 'on' => 'update-photo-upload'],

And use that scenario in controller's action.

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $model->scenario = 'update-photo-upload';
    ........
    .....
}
like image 63
GAMITG Avatar answered Oct 02 '22 00:10

GAMITG