R
R
Ruslan Tilyaev2020-05-03 19:55:36
Yii
Ruslan Tilyaev, 2020-05-03 19:55:36

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(); ?>


Controller
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]);
    }


Model
public function upload()
    {
        if ($this->validate()) {
            debug('+++');
            $this->file->saveAs(Yii::getAlias('@frontend/web/files/specifications/'));
            return true;
        } else {
            return false;
        }
    }


Help me figure it out please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valentyn, 2020-05-03
@Heckfy325

In the upload() method, when validation fails, before the return false; line, check out the errors:
$this->getErrors()
This is in var_dump / debug - which is what you usually use for output.
Next, start from the nature of the errors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question