D
D
dr_loki2017-03-09 09:36:50
Yii
dr_loki, 2017-03-09 09:36:50

Yii2 Undefined variable: model how to fix?

Good afternoon. Started learning YII2
Created a model:
<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
class ViborForm extends Model
{
public $name;
public $items;
public function rules()
{
return [
];
}
}
Created a view with ActivForm.
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
?>
<?php $form = ActiveForm::begin([
'id'=>'filter']);
?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'items[]')->checkboxList(['a' => 'Item A', 'b' =>'Item B', 'c' => 'Item C']);?>
<?= HTML::submitButton("SUBMIT!", ['class' => 'f_ok']) ?>
<?php ActiveForm::end(); ?>
Registered action in the controller.
public function actionVibor()
{
$model = new ViborForm();
return $this->render('vibor',['model' => $model,
]);
}
Through everything is displayed.
When trying to render the form in index.php
<?= $this->render('vibor') ?>
writes Undefined variable: model.
Tell me what's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2017-03-09
@masterfreelance

$this->render('vibor') - renders the view without passing variables to the view.
$this->render('vibor',['model' => $model,]); - here the same thing, but with the transfer of the model variable to the view.
Undefined variable: model You are rendering a view that uses the model variable, and you are not passing the variable itself to the view.
public function actionVibor()
{
$model = new ViborForm();
return $this->render('vibor',['model' => $model,
]);
}
Through everything is displayed.
// Here you pass the model variable and all is well.
When trying to render a form in index.php
<?= $this->render('vibor') ?>
//The variable is not passed here. And accordingly Undefined variable: model. To pass a variable, call $this->render('index', ['model' => $model]) and then in index.php $this->render('vibor', ['model' => $ model])

D
dr_loki, 2017-03-09
@dr_loki

Undefined variable: model
error remains only if
$this->render('vibor') - error when calling vibor.php view
$this->render('vibor',['model' => $model,]);- error when calling the index.php view
A solution was found. (I don’t know how correct this is), but

public function actionIndex()
    {
        $model = new ViborForm();
        return $this->render('index',[
            'model' => $model,
        ]);

    }

that's how it works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question