Francis
Francis

Reputation: 289

Yii2 Call to a member function field() on a non-object

I wanna show all usernames in User tables of Database. It show

Call to a member function field() on a non-object

in the line <?= $form->field($model, 'id')->dropDownList(

When end user click a button to add User into Project, a pop-up will appeared This my code in file popup:

<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use app\modules\admin\models\User;
?>
<select name="from" id="multiselect" class="form-control" size="8" multiple="multiple">
  <?= $form->field($model, 'id')->dropDownList(
       ArrayHelper::map(User::find()->asArray()->indexBy('id')->where(['status'=>1])->all(),'id','username',
       ['prompt'=>Yii::t('app', 'Select User'), 'data-placeholder'=>Yii::t('app', 'Select User'), 'class' => 'form-control select2me', 'style'=>'width: 100%'] ));
  ?>
</select>

Beside, this popup in Project Assignment Controller, not in Project Controller.

My ProjectAssignmentController:

public function actionPopup() {
        $model = new ProjectAssignment();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->renderPartial('popup', [
                'model' => $model,
            ]);
        }
    }

Upvotes: 3

Views: 5406

Answers (1)

BHoft
BHoft

Reputation: 1663

is the popup the complete code? then you haven't added a $form

$form = ActiveForm::begin([
'method' => 'post',
'action' => ['controller/action'],
]);
... your fields code ...
ActiveForm::end();

the dropDownList renders the select html code itself, so you should have to add it with plain html.

Upvotes: 2

Related Questions