yii2 как получить значение поля title из view

Рейтинг: 0Ответов: 0Опубликовано: 30.04.2023

Помогите, вот моя моделька никак не получается получить значение title возвращает null

<?php
namespace app\models;

use Yii;
use yii\base\Model;
use yii\web\UploadedFile;
use yii\db\ActiveRecord;

class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $imageFile;
    public $title;

    public function rules()
    {
        return [
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, mp4, gif'],
            [['title'], 'string', 'min' => 2, 'max' => 255],
        ];
    }
    
    public function upload()
    {
        $db = Yii::$app->db;
        if ($this->validate()) { // Если валидация вернула false то возвращаем null
            $rand_str = Yii::$app->security->generateRandomString(12);
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . $rand_str . '.' . $this->imageFile->extension);
            $image = '/uploads/' . $this->imageFile->baseName . $rand_str . '.' . $this->imageFile->extension;
            $title = $this->title;
            $db->createCommand('INSERT INTO `post` (`author_id`,`date`,`image`,`title`) VALUES (:author_id,:date,:image,:title)', [
                ':author_id' => '2',':date' => date("Y-m-d H:i:s"),':image' => $image, ':title' => $title,
            ])->execute();
            return true;
        } else {
            return false;
        }
    }
}

А вот SiteController.php

public function actionUpload()
{
    if (Yii::$app->user->isGuest) {
        return $this->render('@app/views/redirect.php');
    }

    $model = new UploadForm();

    if ($model->load(Yii::$app->request->post())) {
        $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
        $model->title = UploadedFile::getInstance($model, 'title');
        if ($model->upload()) {
            // file is uploaded successfully
                return $this->render('@app/views/uploaded.php');
        }
    }

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

Сама вьюшка

<?php

/** @var yii\web\View $this */

use yii\bootstrap5\ActiveForm;
use yii\bootstrap5\Html;

$this->title = 'Загрузить';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
    <h1><?= Html::encode($this->title) ?></h1>

    <p>
    <?php
    if(Yii::$app->user->isGuest)
        return $this->render('@app/views/redirect.php');
    else
        $form = ActiveForm::begin(['id' => 'form-upload']);
    ?>
    <div class="mb-3">
        <?= $form->field($model, 'title')->label('Придумайте заголовок.') ?>
    </div>
    <div class="mb-3">
        <label class="form-label">Вы можете загрузить фото или видео.</label>
        <?= $form->field($model, 'imageFile')->label('')->fileInput() ?>
    </div>
    <div class="col-auto">
        <button type="submit" class="btn btn-primary mb-3">Загрузить</button>
    </div>
    <?php ActiveForm::end() ?>
    </p>
</div>

Ответы

Ответов пока нет.