私はyii2-widget-fileinputをフォームにアップロードしています。 アップロードボタンまたは作成ボタンをクリックすると、コントローラにTrying to get property of non-object
エラーが発生します。Yii2で画像をアップロードするためにkartik-vを使用しているときに非オブジェクトのプロパティを取得しようとしています
コントローラ
public function actionCreate()
{
Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/uploads/';
$model = new Ads();
$provinces = ArrayHelper::map(Province::find()->all(), 'name', 'name');
if ($model->load(Yii::$app->request->post())){
$image = UploadedFile::getInstances($model, 'image');
$model->filename = $image->name;
$ext = end((explode(".", $image->name)));
$avatar = Yii::$app->security->generateRandomString().".{$ext}";
$path = Yii::$app->params['uploadPath'].$avatar;
if ($model->save()) {
$image->saveAs($path);
$model->image_adr = $path;
return $this->redirect(['view', 'id' => $model->id]);
}else{
echo "error on saving the model";
}
}
return $this->render('create', [
'model' => $model,
'provinces'=>$provinces,
]);
}
モデルルール
public function rules()
{
return [
[['type', 'explanation', 'cost', 'province_name', 'address'], 'required'],
[['type', 'explanation', 'image_adr', 'address'], 'string'],
[['cost'], 'integer'],
[['province_name'], 'string', 'max' => 20],
[['province_name'], 'exist', 'skipOnError' => true, 'targetClass' => Province::className(), 'targetAttribute' => ['province_name' => 'name']],
[['image'],'safe'],
[['image'], 'file', 'extensions'=>'jpg, gif, png', 'maxFiles'=>3,],
];
とfinnally 図
<?= $form->field($model, 'image[]')->widget(FileInput::classname(), [
'options'=>['accept'=>'image/*', 'multiple'=>true],
'pluginOptions'=>['allowedFileExtensions'=>['jpg','gif','png'], 'overwriteInitial'=>false,]
]); ?>
問題画像はポストにかそうでない私は、エラーの画像は参考になりまし
エラーが示すように、 '$ image = UploadedFile :: getInstances($ model、 'image');'はあなたが望むものを返さず、画像がポストにはありません。私は何をすべきか? – Amir
あなたのフォームにencytype multipartデータが追加されていますか? –
実際には...どこに追加すればいいですか? – Amir