The situation is as follows. There is a file field that loads the file and saves the file name to the database. If I edit an entry that already contains a file that was previously uploaded, but leaves the file field intact (for example, only the title needs to be changed), then the corresponding field in the database becomes empty. In addition, if the field is required, the form generally refuses to be sent, requiring you to upload a new file. What is the right thing to do in this situation?
Update : I will add the code of my actionUpdate, which would be clearer (on the advice of @YaroslavMolchan).
public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { $directory = Service::getFileDirectory(); $mainImage = UploadedFile::getInstance($model, 'main_image'); if ($mainImage) { $uid = uniqid(time(), true); $model->main_image = $uid . '.' . $mainImage->extension; $mainImage->saveAs($directory . $model->main_image); } if ($model->save()) return $this->redirect(['view', 'id' => $model->id]); } return $this->render('update', [ 'model' => $model, ]); }