Answer the question
In order to leave comments, you need to log in
Why does it say cannot be blank(when uploading a file)?
Here is a function in the controller responsible for creating a record
public function actionCreate()
{
$model = new Book();
$model->scenario = 'create';
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->preview = UploadedFile::getInstance($model, 'preview');
$model->save();
$model->preview->saveAs('uploads/' . $model->preview->baseName . '.' . $model->preview->extension);
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
public function rules()
{
return [
[['name', 'preview', 'date', 'author_id'], 'required', 'on' => 'update'],
[['name', 'preview', 'date', 'author_id'], 'required', 'on' => 'create'],
[['date_create', 'date_update', 'author_id'], 'integer'],
[['preview'], 'file', 'skipOnEmpty' => 'false', 'extensions' => 'png, jpg, jpeg'],
[['date'], 'safe'],
[['name'], 'string', 'max' => 255]
];
}
public function actionCreate()
{
$model = new Book();
//$model->scenario = 'create';
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->preview = UploadedFile::getInstance($model, 'preview');
$model->preview->saveAs('uploads/' . $model->preview->baseName . '.' . $model->preview->extension);
if ($model->save())
{
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Answer the question
In order to leave comments, you need to log in
1) Because your preview field is required
2) you may not have it specified in the form - enctype="multipart/form-data" , check the incoming data
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question