0
2つのyiiプロジェクトで同様のコードを使用します。 1つはかなりうまく動作し、もう1つはうまく動作しません。 Foreach-Loopは入力されませんが、メソッドは実行されます。 これはどのように修正するのですか? マイモデル: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)') ?>