在 Yii2 中更新图像 [英] Updating Image in Yii2
本文介绍了在 Yii2 中更新图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用 Yii2 更新图像时,我遇到了验证问题.它总是要求我上传图像.但我不想要这个.不需要总是更新图像.
While updating image using Yii2 I'm facing a problem with the validation.Its always asking me to upload an image. But I don't want this. Always updating an image is not necessary.
我尝试了 skipOnEmpty
但它不能正常工作,它会在上传照片时产生影响,这也是不正确的.
I tried skipOnEmpty
but its not working properly it cause effect while uploading a photo, which is also incorrect.
请帮忙!!
型号
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],
];
}
控制器
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,
]);
}
}
推荐答案
你应该使用scenario
进行更新.
就像,在应用 scenario
的模型规则中添加 on
条件.
Like as,
Add on
condition in model's rule for applying scenario
.
[['imageFiles'], 'image','extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 4, 'minWidth' => 100, 'maxWidth' => 800, 'minHeight' => 100, 'maxHeight'=>600,'skipOnEmpty' => true, 'on' => 'update-photo-upload'],
并在控制器的操作中使用该scenario
.
And use that scenario
in controller's action.
public function actionUpdate($id)
{
$model = $this->findModel($id);
$model->scenario = 'update-photo-upload';
........
.....
}
这篇关于在 Yii2 中更新图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文