Answer the question
In order to leave comments, you need to log in
Why does validation fail when uploading a file to the server?
Hello, I'm uploading a file to a local server, but the validation fails in upload(). I've been suffering for half a day now. It seems that everything is correct.
Here is my form
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'file')->fileInput()->label(false) ?>
<?= $form->field($model, 'name')->input('text')->label(false) ?>
<?= $form->field($model, 'phone')->input('text')->label(false) ?>
<?= $form->field($model,'message')->textarea(['class' => 'form-control', 'rows' => '4'])->label(false)?>
<?php ActiveForm::end(); ?>
public function actionSpecification()
{
$model = new Specifications();
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->upload()) {
// file is uploaded successfully
Yii::$app->session->setFlash('success', 'Is work!');
return true;
}
}
return $this->render('specification', ['model' => $model]);
}
public function upload()
{
if ($this->validate()) {
debug('+++');
$this->file->saveAs(Yii::getAlias('@frontend/web/files/specifications/'));
return true;
} else {
return false;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question