Why do I get an error with no definition of the $model variable in the YII2 active form

I always get the error about the undefined $model variable when using Active Form. Please guide me

I have 2 files as below:

LoginController.php

<?php
namespace app\controllers;
use app\models\User;
class LoginController extends \yii\web\Controller
{
    public $model;
    public function actionIndex()
    {
        $this->model = new User();
        return $this->render('index', array(
            '$model' => $this->model
        ));
    }
}

login/index.php

<?php
// Su dung wiget Active form
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<?php $form1=ActiveForm::begin()?>
    <?=$form1->field($model, $attribute);?>
    <?=Html::submitButton("Login",['class'=>"btn"] )?>
<?php $form = ActiveForm::end()?>

Upvotes: 0

Views: 40

Answers (1)

Juan Antonio
Juan Antonio

Reputation: 2624

Your code is wrong, must be without $, and ca be passed direcctly without array word.

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

Take a look to docs.

Upvotes: 1

Related Questions