如何在yii2中验证图像更新


How to validate image on update in yii2

我在yii中对图像进行了验证。它在记录创建期间工作,但在更新表单时也会验证图像字段。我不想每次都更新图像。如何制定规则呢?谢谢。

这是非常容易的,而更新文件或图像。你需要为此创建一个场景,例如,如果你有规则。

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

然后在你的控制器上创建操作使用这个代码。

    $model->setScenario('create');

相当简单。