不会进入foreach循环
Foreach loop won't be entered
我在两个 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)') ?>
$this->fileImage
可能是空的,因为您没有验证输入。
我在两个 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)') ?>
$this->fileImage
可能是空的,因为您没有验证输入。