gpt4 book ai didi

yii - `Skip on empty` Yii2文件上传不工作

转载 作者:行者123 更新时间:2023-12-04 13:27:04 25 4
gpt4 key购买 nike

我有一条规定可以在我的应用程序中为公司上传 Logo 。在创建配置文件时上传和保存工作正常。但是在更新时,如果我不再上传, Logo 就会变为空!

这是我的更新表格

    <?php $form = ActiveForm::begin([
'options' => ['enctype'=>'multipart/form-data']
]); ?>

.....

<?= $form->field($model, 'logo')->fileInput() ?>

...

我的 Controller Action
    if ($model->load($_POST) ) {
$file = \yii\web\UploadedFile::getInstance($model, 'logo');
if($file){
$model->logo=$file; }

if($model->save()){

if($file)
$file->saveAs(\Yii::$app->basePath . '/web/images/'.$file);
}

return $this->redirect(['profile']);
} else {
return $this->renderPartial('update', [
'model' => $model,
]);
}

我的规则:
public function rules()
{
return [

[['logo'], 'image', 'extensions' => 'jpg,png', 'skipOnEmpty' => true],
[['name'], 'required'],
[['name', 'description'], 'string'],

];
}

有任何想法吗????

最佳答案

'skipOnEmpty' => !$this->isNewRecord

对于更新,可以跳过它。

关于yii - `Skip on empty` Yii2文件上传不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25237661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com