Why i'm Getting Call to a member function formName() on null in yii2

670 views Asked by At

I can't find any supporting resources for the error Call to a member function formName() on null. The scenario of getting this error when opening a update page. I've checked the rendered variable on update page by print_r() and its fine. Then I checked by removing the all input field which codes in yii2 ActiveFrom chtml

<?= $form->field($model, 'name')->textInput(['maxlength' => true, 'class' => ' form-control']) ?> 

and kept some input fields which is in normal html syntax

<input type="text" class="qty" name="qty[]" value="1" style="width: 24px;" value="<?php echo $value->qty; ?>">

Then it worked. Why this error occurred and how to fix it. Thanks,

1

There are 1 answers

0
Michal Hynčica On

You are probably not passing an update model from controller's action to view. Make sure you pass the loaded model in your update action to view like this:

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    //... other update action code

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

If you are using some common _form template that you include into your create/update templates you also have to make sure the $model is passed from your update template to the _form template.

<!-- ... your update template code ... -->
<?= $this->render('_form', ['model' => $model]); ?>
<!-- ... the rest of update template ... -->

Another option is that you are overwiriting the $model variable somewhere in your templates.