JKLM
JKLM

Reputation: 1520

Yii2: How to skip empty form filed while uploading image?

I need to skip particular file upload if user did not fill that form field, for example I have 4 fields to upload an image if user fill only 3 fields, then controller should skip 4th form filed. Getting error Call to a member function saveAs() on null because controller is not skipping the empty form filed.

How to skip empty form filed ?

public function actionCreate()
    {
        $model = new HomePageImg();

        if ($model->load(Yii::$app->request->post())) 
        {
            $imageName1 = $model->img_1_name;
            $imageName2 = $model->img_2_name;
            $imageName3 = $model->img_3_name;
            $imageName4 = $model->img_4_name;

            $model->file1 = UploadedFile::getInstance($model, 'file1');
            $model->file1->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName1 . '.' . $model->file1->extension);
            //save the path in the db column
            $model->photo_1 = 'uploads/hotel-home-img/' . $imageName1 . '.' . $model->file1->extension;

            $model->file2 = UploadedFile::getInstance($model, 'file2');
            $model->file2->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName2 . '.' . $model->file2->extension);
            //save the path in the db column
            $model->photo_2 = 'uploads/hotel-home-img/' . $imageName2 . '.' . $model->file1->extension;

            $model->file3 = UploadedFile::getInstance($model, 'file3');
            $model->file3->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName3 . '.' . $model->file3->extension);
            //save the path in the db column
            $model->photo_3 = 'uploads/hotel-home-img/' . $imageName3 . '.' . $model->file1->extension;

            $model->file4 = UploadedFile::getInstance($model, 'file4');
            $model->file4->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName4 . '.' . $model->file4->extension);
            //save the path in the db column
            $model->photo_4 = 'uploads/hotel-home-img/' . $imageName4 . '.' . $model->file1->extension;

            $model->save(false);

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

Upvotes: 0

Views: 896

Answers (3)

Midhun
Midhun

Reputation: 3998

You can check it like this

if(UploadedFile::getInstance($model, 'file2')!==null)
{

}

Let me help you with the code

public function actionCreate()
        {
        $model = new HomePageImg();

        if ($model->load(Yii::$app->request->post())) 
        {
            $imageName1 = $model->img_1_name;
            $imageName2 = $model->img_2_name;
            $imageName3 = $model->img_3_name;
            $imageName4 = $model->img_4_name;
            if( UploadedFile::getInstance($model, 'file1')!==null )
            {
                $model->file1 = UploadedFile::getInstance($model, 'file1');
                $model->file1->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName1 . '.' . $model->file1->extension);
                //save the path in the db column
                $model->photo_1 = 'uploads/hotel-home-img/' . $imageName1 . '.' . $model->file1->extension;
            }

            if( UploadedFile::getInstance($model, 'file2')!==null )
            {
                $model->file2 = UploadedFile::getInstance($model, 'file2');
                $model->file2->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName2 . '.' . $model->file2->extension);
                //save the path in the db column
                $model->photo_2 = 'uploads/hotel-home-img/' . $imageName2 . '.' . $model->file1->extension;
            }
            if( UploadedFile::getInstance($model, 'file3')!==null )
            {
                $model->file3 = UploadedFile::getInstance($model, 'file3');
                $model->file3->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName3 . '.' . $model->file3->extension);
                //save the path in the db column
                $model->photo_3 = 'uploads/hotel-home-img/' . $imageName3 . '.' . $model->file1->extension;
            }
            if(UploadedFile::getInstance($model, 'file4')!==null )
            {
                $model->file4 = UploadedFile::getInstance($model, 'file4');
                $model->file4->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName4 . '.' . $model->file4->extension);
                //save the path in the db column
                $model->photo_4 = 'uploads/hotel-home-img/' . $imageName4 . '.' . $model->file1->extension;
            }

            $model->save(false);

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    } 

Upvotes: 2

rakhmatov
rakhmatov

Reputation: 377

$model->file3 = UploadedFile::getInstance($model, 'file3');
if ($model->file3 === true) {
    $model->file3->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName3 . '.' . $model->file3->extension);
}

Upvotes: 0

topher
topher

Reputation: 14860

Just check if the object is null before the call to saveAs:

$model->file3 = UploadedFile::getInstance($model, 'file3');
if ($model->file3) { // is_null($model->file3) will also work
    $model->file3->saveAs(Yii::getAlias('@frontend/web') . '/uploads/hotel-home-img/' . $imageName3 . '.' . $model->file3->extension);
}

Upvotes: 0

Related Questions