Khizar Hayat
Khizar Hayat

Reputation: 3

How to validate image on update in yii2

I have put validation in yii for images. It works during record creation but when updating the form it also validates the image field. I don't want to update the image every time. How can the rules be set for this? Thanks.

Upvotes: 0

Views: 927

Answers (1)

mutahir
mutahir

Reputation: 107

this is very easy while , updating the file or image. You need to create a scenario for this e.g if you have rules.

public function rules()
    {
          return [
            [['contents'], 'string'],
            [['status', 'parent_id', 'sort_order'], 'integer'],
            [['status','title'], 'required'],
            [['image'], 'required', 'on' => 'create'],
            [['title', 'slug'], 'string', 'max' => 255],
        ];
    }

then in your Controller on create action use this code.

    $model->setScenario('create');

pretty simple.

Upvotes: 1

Related Questions