I have trouble uploading files. Press Save. Then message error "Bad Request (# 400) Missing required parameters: id".
I had tried by doing $ model-> save (false); but when I do so, only uploads the text, and not the images.
File backend\views\album\ _form.php
<?= $form->field($model, 'file')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*'],
'pluginOptions' => [
'initialPreview'=>[],
'allowedFileExtensions'=>['jpg', 'png','gif'],
'showPreview' => true,
'showRemove' => true,
'showUpload' => false
]
]); ?>
File backend\models\Album.php
public static function tableName()
{
return 'album';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['album_name', 'album_detail', 'album_cover', 'album_create', 'album_status'], 'required'],
[['album_create'], 'safe'],
[['album_status'], 'string'],
[['file'],'file'],
[['album_name', 'album_cover'], 'string', 'max' => 100],
[['album_detail'], 'string', 'max' => 255],
];
}
File backend\controllers\AlbumController.php
public function actionCreate()
{
$model = new Album();
if ($model->load(Yii::$app->request->post()))
{
// get the instance of the uploaded file
$imageName = $model->album_name;
$model->file = UploadedFile::getInstance($model,'file');
$model->file->saveAs( 'uploads/'.$imageName.'.'.$model->file->extension );
// save the path in the db column
$model->album_cover = 'uploads/'.$imageName.'.'.$model->file->extension;
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
</div>
In actionCreate()
u're not checking if save()
was successfull. Add:
if($model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
Remove else
statement from:
else {
return $this->render('create', [
'model' => $model,
]);
}
Leave just:
return $this->render('create', [
'model' => $model,
]);
Propably your model is not saving, running it like u said $model->save(false)
save it without any validation. Try to debug why it's not saving, in easiest way, after $model->save()
or $model->validate()
:
var_dump($model->errors);die;