Rajinder
Rajinder

Reputation: 1091

"Undefined variable" error, when rendering data in a view

I want to render static array in _form.php view, using actionCreate method in Yii2. Here is my code:

if ($model->load(Yii::$app->request->post()) && $model->save()) {
     return $this->redirect(['view', 'id' => $model->id]);
} else {
    $data = array('1'=>'AA','2'=>'BB');

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

When I try to display this data in _form.php view, I am getting an error "Undefined variable: data".

Here is my _form.php code:

<?= $form->field($model, 'fixer_type[]')->dropDownList($data,['prompt'=>'Select Fixer Trade']) ?>

What am I missing?

Upvotes: 2

Views: 2101

Answers (1)

Dency G B
Dency G B

Reputation: 8146

You are passing the $data to create.php.Render the same from your create.php to make it available in your form.php.

In your create.php

<?= $this->render('_form', [
    'model' => $model,
    'data' => $data ,
]) ?>

Upvotes: 5

Related Questions