duan201444 2019-08-14 07:23
浏览 260

Yii2文件验证始终显示为空

I have a Kartik fileInput in my form and here's how the code for making it looks like:

echo $form->field($model, 'images')->widget(FileInput::classname(), [
    'options' => ['accept' => 'image/*','multiple' => true]
]);

I want to validate the input so that uploading images is required. For that I have the following line in my model:

[['images'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg,jpeg']

This is the code of my model:

<?php

namespace app\models;

use Yii;
use yii\web\UploadedFile;

/**
 * LoginForm is the model behind the login form.
 *
 * @property User|null $user This property is read-only.
 *
 */
class PhonesForm extends \yii\db\ActiveRecord
{
    public $images;

    public static function tableName()
    {
        return '{{phones}}';
    }

    /**
     * @return array the validation rules.
     */ 
    public function rules()
    {
        return [
            [['name', 'price','model','description'], 'required'],
            [['name','model'],'string', 'min' => 3],
            [['description'],'string', 'min' => 25],
            ['price','integer'],
            [['images'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg,jpeg']
        ];
    }

    public function upload()
    {
        if ($this->validate()) {
            $this->images->saveAs('uploads/' . $this->images->baseName . '.' . $this->images->extension);
            return true;
        } else {
            return false;
        }
    }
}

The problem is that is always gives me a validation error saying Please upload a file.. How can I fix this problem? Thanks.

UPDATE

I have updated my controller method and my model.

Controller method:

public function actionAdd()
    {
        if (!Yii::$app->user->getIsGuest() && Yii::$app->user->identity->role == 'admin') {

            $model = new PhonesForm();

            if ($model->load(Yii::$app->request->post())) {

                $model->images = UploadedFile::getInstances($model, 'images');

                if ($model->upload()) {
                    echo "success";
                }
                else{
                    return $this->render('dashboardAdd',[
                        'model' => $model
                    ]);
                }
            }
            else{
                return $this->render('dashboardAdd',[
                    'model' => $model
                ]);
            }   
        }
        else{
            return $this->redirect(['site/index']);
        }
    }

Model:

<?php

namespace app\models;

use Yii;

/**
 * LoginForm is the model behind the login form.
 *
 * @property User|null $user This property is read-only.
 *
 */
class PhonesForm extends \yii\db\ActiveRecord
{
    public $images;

    public static function tableName()
    {
        return '{{phones}}';
    }

    /**
     * @return array the validation rules.
     */ 
    public function rules()
    {
        return [
            [['name', 'price','model','description'], 'required'],
            [['name','model'],'string', 'min' => 3],
            [['description'],'string', 'min' => 25],
            ['price','integer'],
            [['images'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg,jpeg']
        ];
    }

    public function upload()
    {
        if (!$this->validate()) { 
            return false;
        }
        foreach ($this->images as $image) {
            $image->saveAs('uploads/' . $image->baseName . '.' . $image->extension);
        }
        return true;
    }
}

I still have the same issue.

  • 写回答

1条回答 默认 最新

  • douxi4114 2019-08-14 07:44
    关注

    Before validation, you must pass uploaded files to the model. For example, you can do it in your controller

    $model = new PhonesForm();
    
    if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
        $model->images = UploadedFile::getInstances($model, 'images');
        if ($model->save() && $model->upload()) {
            return;
        }
     }
    

    But if you want to upload multiple files upload method must work with files as an array, for example:

    public function upload()
    {
        if (!$this->validate()) { 
            return false;
        }
    
        foreach ($this->images as $image) {
            $image->saveAs('uploads/' . $image->baseName . '.' . $image->extension);
        }
        return true;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)