Yii2 ActiveForm 上传不同属性的文件

我有一个 Yii2 ActiveForm,它有两个接受文件输入的表单域。


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

<?= $form->field($model, 'productImage[]')->fileInput() ?>

在控制器中,我有:


    public function actionCreate()

{

    $model = new Product();

    $model->supplier_id = Yii::$app->user->identity->id;


    $imageArray = ['mainImage','productImage'];

    $mainImageIndex = 1;

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


        $model->mainImage = UploadedFile::getInstance($model, 'mainImage');

        $model->images = $model->singleImageUpload();

        //UploadedFile::reset();


        // var_dump($model->mainImage);

        // exit();


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

        $images = $model->multipleImageUpload();

        $imageCount = count($images);





        if ($model->validate() && $model->save(false)) { 


            for ($i=0; $i < $imageCount; $i++) { 

                $imageModel = new ProductImage();

                $imageModel->product_id = $model->id;

                $imageModel->image = $images[$i];

                $imageModel->save();

            }


            return $this->redirect(['view', 'id' => $model->id]);


        }


    }


    return $this->render('create', [

        'model' => $model,

    ]);

}

在 Model 类中,我有这些验证规则:


[['mainImage','productImage'], 'safe'],

[['mainImage','productImage'], 'file','skipOnEmpty' => true, 'extensions' => 'jpeg, jpg, png','checkExtensionByMimeType'=>false, 'maxFiles'=>10],

每次我提交表单时,我都会收到一个mainImage字段错误,说明Please upload a file. 没有错误productImage 可能有什么解决方法?


编辑

图像mainImage并productImage成功上传,但验证错误仍然存在mainImage


慕容森
浏览 173回答 2
2回答

慕盖茨4494581

问题是模型类中的验证。我已经将它们标记为safe[['mainImage','productImage'],&nbsp;'safe'],我注释掉了这个验证并且它起作用了

慕田峪7331174

如果要上传多个文件,则需要更改视图<?= $form->field($model, 'mainImage')->fileInput() ?><?= $form->field($model, 'productImage[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>并且您的ruleinmodel说您一直在尝试上传多个图像的属性productImage[]和mainImage. 但是视图表示该属性mainImage用于单个文件,因此更改规则。[['mainImage'], 'file', 'skipOnEmpty' => true, 'extensions' => 'jpeg, jpg, png'],[['productImage'], 'file','skipOnEmpty' => true, 'extensions' => 'jpeg, jpg, png','checkExtensionByMimeType'=>false, 'maxFiles'=>10],在控制器中,$model->productImage = UploadedFile::getInstances($model, 'productImage');$model->mainImage = UploadedFile::getInstance($model, 'mainImage');if ($model->multipleImageUpload()) {&nbsp; &nbsp;// file is uploaded successfully&nbsp; &nbsp;return;}在你的模型中,public function multipleImageUpload()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($this->validate()) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($this->imageFiles as $file) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }如需更多参考,请访问此链接:https : //www.yiiframework.com/doc/guide/2.0/en/input-file-upload
打开App,查看更多内容
随时随地看视频慕课网APP