tklustig
tklustig

Reputation: 503

Foreach loop won't be entered

I use similar code in two yii-projects. The one works pretty fine, the other doesn't. Foreach-Loop won't be entered,although method will. Any ideas, how to fix this? My model:

<?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
?>

My Controller:

    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]);
    }

And my View:

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

Upvotes: 0

Views: 35

Answers (1)

Bizley
Bizley

Reputation: 18021

$this->fileImage is probably empty because you are not validating the input.

Upvotes: 1

Related Questions