Выбивает ошибку "Call to a member function isAttributeRequired() on null" при загрузке файла

Собственно делал по инструкции от сюда https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload

Создал файл models/UploadForm.php:

<?
namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

/**
 * UploadForm is the model behind the upload form.
 */
class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $imageFile;

    public function rules()
    {
        return [
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
        ];
    }
    
    public function upload()
    {
        if ($this->validate()) {
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
        } else {
            return false;
        }
    }
}

В viewes/index.php вставил:

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

    <?= $form->field($model, 'imageFile')->fileInput() ?>

    <button>Submit</button>

<?php ActiveForm::end() ?>

В конце controllers/SiteController.php добавил:

public function actionUpload()
{
    $model = new UploadForm();

    if (Yii::$app->request->isPost) {
        $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
        if ($model->upload()) {
            // file is uploaded successfully
            return;
        }
    }

    return $this->render('upload', ['model' => $model]);
}

Вроде всё как указано в инструкции, но один буй ссылается на атрибуты. Что я всё таки сделал не так?


Ответы (0 шт):