Khizar Hayat Khizar Hayat - 9 months ago 54
PHP Question

How to validate image on update in yii2

I have put validation in yii for images , it works in during record creation but when update the form it also validate the image field , I want not to update the image every time, how the rules can be set for this.


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.


pretty simple.