2

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?

1 Answer 1

5

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 ,
]) ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.