M
M
MaikMain2022-01-19 16:39:54
Yii
MaikMain, 2022-01-19 16:39:54

How to include file and pass model to Yii2?

Hello everyone, I've run into this problem. Making a callback function
I have a model (Callback), a controller (CallbackController) and a view (frontend/view/callback/index.php).
Controller:

spoiler
public function actionIndex()
    {
        $model = new Callback();

        if ($this->request->isPost) {
            if ($model->load($this->request->post()) && $model->save()) {
                Yii::$app->session->setFlash('success', 'Сообщение успешно отправлено!');
                return $this->redirect(Yii::$app->request->referrer);
            }
        }
        return $this->render('index', compact('model'));
    }

I'm trying to output (connect) this callback/inde.php to the layout footer
<?php echo $this->renderA('@frontend/views/callback/index.php'); ?>

callback/index.php
spoiler
<?php \yii\widgets\Pjax::begin(); ?>
<?php $form = \yii\bootstrap\ActiveForm::begin(['id' => 'callback-form-index', 'action' => ['callback/index'],]); ?>
                <div class="modal-body">
                    <?= $form->field($model, 'name')->textInput(['maxlength' => true, 'placeholder' => 'Ваше имя']) ?>
                    <?= $form->field($model, 'phone')->textInput() ?>
                    <?= $form->field($model, 'reCaptcha')->widget(
                        \himiklab\yii2\recaptcha\ReCaptcha2::class,
                        [
                            'siteKey' => 'мой ключ, // unnecessary is reCaptcha component was set up
                        ]
                    ) ?>
                </div>
                <div class="modal-footer">
                    <?= \yii\helpers\Html::submitButton('Заказать обратный звонок', ['class' => 'callback_btn', 'name' => 'callback-button', 'id' => 'refreshButton1', 'onclick' => "yaCounter51997982.reachGoal('buttonCallback'); return true;"]) ?>
                </div>
                <?php \yii\bootstrap\ActiveForm::end(); ?>
<?php \yii\widgets\Pjax::end(); ?>


But I'm getting an error:
Call to a member function isAttributeRequired() on null

Swears on fields which the user should fill.
Please tell me how this can be overcome.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IvanMogilev, 2022-01-19
@MaikMain

<?php echo $this->renderPartial('@frontend/views/callback/index.php',["model" =>new Callback()]); ?>

try passing the model in the place where you render, only you will need to specify the full namespace before the model. Your controller renders a specific page, and it does not know that you are inserting a template into the footer. After submitting the form, you will get into it, but for rendering, the object must be passed separately

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question