Answer the question
In order to leave comments, you need to log in
How to properly pass model to layout in yii2?
I have a feedback form in layout, and I need to pass the model there.
I was looking for options and the option with the widget seems to me the most correct, but I don’t know how to return the model from the widget or I simply don’t understand how it should work.
Here is the widget code:
<?php
namespace frontend\widgets;
use yii\base\Widget;
use yii\helpers\Html;
use common\models\Callback;
class LModel extends Widget {
public $lModel;
public function init()
{
parent::init();
}
public function run()
{
$lModel = new Callback();
return $lModel;
}
}
Answer the question
In order to leave comments, you need to log in
public function run() {
return $this->render('callbackform', [
'model' => new Callback(),
]);
}
<?php
use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<div id="callback">
<?php $form = ActiveForm::begin([
'id' => 'callback-form',
'enableAjaxValidation' => true,
'action' => Url::to(['site/callback']),
'validationUrl' => Url::to(['site/callback']),
]); ?>
<?= $form->field($model, 'name')->textInput([
'placeholder' => 'Имя',
])->label(''); ?>
<?= $form->field($model, 'phone')->textInput([
'placeholder' => 'Телефон',
])->label(''); ?>
<?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
<?php $form->end(); ?>
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question