在Yii2中更新图像

San*_*Pal 7 yii2

使用Yii2更新图像时,我遇到验证问题.它总是要求我上传图像.但我不想要这个.始终无需更新图像.

我试过skipOnEmpty但它不能正常工作它会在上传照片时产生效果,这也是不正确的.

请帮忙!!

模型

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],


        ];
    }
Run Code Online (Sandbox Code Playgroud)

调节器

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,
            ]);
        }
    }
Run Code Online (Sandbox Code Playgroud)

GAM*_*ITG 4

您应该用于scenario更新。

on就像as一样,在模型的规则中添加条件来应用scenario

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

scenario并在控制器的操作中使用它。

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $model->scenario = 'update-photo-upload';
    ........
    .....
}
Run Code Online (Sandbox Code Playgroud)