0

I have a create action of controller where I passed $ingredients array into the view

public function actionCreate()
{
    return $this->render('create', 
        ['model' => $model, 'ingredients' => Ingredient::find()->all()]
    );
}

In create.php I can acess $ingredients variable. But if I try to access $ingredients in the _form.php

<?= $form->field($model, 'ingredient_id')->checkbox(
    ArrayHelper::map($ingredients, 'id', 'name')
) ?> 

I get error

Undefined variable: ingredients

3 Answers 3

1

This can be done by two ways
//first render the view file like this

    $params = [];
    $params['eqpType'] = $eqpType;
    $params['discountModel'] = $discountModel;
    $params['tabActive'] = $tabActive;
    $params['discountLabel'] = $discountLabel;

    echo $this->render('_form', $params);

//or you can include the code file directly ..in this case do not need to pass all perameters

    <?php include_once "_form.php";?>
Sign up to request clarification or add additional context in comments.

2 Comments

The first approach helped
And how I can get in controller an array of checkboxes if name of checkbox is Dish[ingredients][] ?
1

Your are passing the variables to the create view, and your widget is in another view called _form. Go to the create view and make sure that all variables are passed to the _form view.

Comments

1

in create.php

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

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.