Цикл Foreach не будет введен

Я использую похожий код в двух yii-проектах. Один работает довольно хорошо, другой нет. Цикл Foreach не будет введен, хотя метод будет. Любые идеи, как это исправить? Моя модель:

<?php
namespace app\models;

use Yii;
use yii\base\Model;
use kartik\widgets\Growl;

class myScriptForm extends Model {


    public $fileImage;

    public function rules() {
        return [
            [['fileImage'], 'file', 'skipOnEmpty' => false, 'maxFiles' => 3],
            ['fileImage', 'required'],
        ];
    }

    public function attributeLabels() {

        return ['fileImage' => 'Image'];
    }

    public function upload() {
        echo Growl::widget([ //This output will be shown
                'type' => Growl::TYPE_SUCCESS,
                'title' => 'Well done!',
                'icon' => 'glyphicon glyphicon-ok-sign',
                'body' => 'File(s) successfully uploaded<br> It is available in folder uploadedfiles',
                'showSeparator' => true,
                'delay' => false,
                'pluginOptions' => [
                    'showProgressbar' => true,
                    'placement' => [
                        'from' => 'top',
                        'align' => 'center',
                    ]
                ]
            ]);
        foreach ($this->fileImage as $uploaded_file) {
            echo Growl::widget([ //This output will not be shown -WHY??
                'type' => Growl::TYPE_SUCCESS,
                'title' => 'Well done!',
                'icon' => 'glyphicon glyphicon-ok-sign',
                'body' => 'File(s) successfully uploaded<br> It is available in folder uploadedfiles',
                'showSeparator' => true,
                'delay' => false,
                'pluginOptions' => [
                    'showProgressbar' => true,
                    'placement' => [
                        'from' => 'top',
                        'align' => 'center',
                    ]
                ]
            ]);
            $uploaded_file->saveAs(Yii::getAlias('@uploadedfilesdir') . '/' . $uploaded_file->baseName . '.' . $uploaded_file->extension);
        }
        return true;
    }

}

//End of class
?>

Мой контроллер:

    public function actionScript() { //A new method, programmed by Thomas Kipp
        try {
            $model = new myScriptForm();
        } catch (InvalidParamException $error) {
            throw new BadRequestHttpException($error->getMessage());
        }
        if ($model->load(Yii::$app->request->post())) {
            $model->fileImage = UploadedFile::getInstances($model, 'fileImage');
            if ($model->upload()) {
                return $this->render('myScript', ['model' => $model]);
            }
        }
        return $this->render('myScript_Formular', ['model' => $model]);
    }

И мой взгляд:

<?= $form->field($model, 'fileImage[]')->fileInput(['multiple' => true,])->label('Uploadfile(s)') ?>

1 ответ

Решение

$this->fileImage вероятно, пусто, потому что вы не проверяете ввод.

Другие вопросы по тегам