使用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],];
- }
调节器
- 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,]);
- }
- }
您应该使用方案进行更新.
像,
在模型的规则中添加条件以应用场景.
- [['imageFiles'],'skipOnEmpty' => true,'on' => 'update-photo-upload'],
并在控制器的操作中使用该场景.
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
- $model->scenario = 'update-photo-upload';
- ........
- .....
- }