P
P
Pavel Ivanov2014-09-17 09:18:44
Yii
Pavel Ivanov, 2014-09-17 09:18:44

What is causing the "Call to a member function formName() on a non-object" error in Yii 2.0?

Good afternoon!
I have a loginold.php form:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $form yii\widgets\ActiveForm */
/* @var $model \common\models\LoginForm */

$this->title = 'Login';
?>
<div class="site-login">
    <h1><?= Html::encode($this->title) ?></h1>

    <p>Please fill out the following fields to login:</p>

    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
                <?= $form->field($model, 'username') ?>
                <?= $form->field($model, 'password')->passwordInput() ?>
                <?= $form->field($model, 'rememberMe')->checkbox() ?>
                <div style="color:#999;margin:1em 0">
                    If you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>.
                </div>
                <div class="form-group">
                    <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
                </div>
            <?php ActiveForm::end(); ?>
        </div>
    </div>
</div>

When I try to access it from the controller:
public function actionIndex()
    {
        $r = Yii::$app->request;

        if (\Yii::$app->user->isGuest) {
            return $this->render('loginold');
        }
        else {
            return $this->goBack();

        }
    }

then I get an error:
Call to a member function formName() on a non-object

public static function getInputName($model, $attribute)
    {
        $formName = $model->formName();

What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-01-27
@Jekshmek

You pass your model to the view
$model=myModel();
return $this->render('loginold','model' => $model);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question