P
P
Pavel Stepantsov2020-04-17 08:35:12
Yii
Pavel Stepantsov, 2020-04-17 08:35:12

Yii2 dynamically created dropdown lists via foreach?

Hello, I need advice... Probably even an explanation...
Guided by this article

With this representation of _form:

foreach ($tableFromTemplate as $index => $setting) {
        echo $form->field($setting, '[$index]id_table')->dropDownList(
        ArrayHelper::map($tableDesc, 'id', 'tag_table_desc')
    )->label(false);

in the foreach in the _form view, when building the dom, the key is passed as [$index]... Accordingly, I can't save the changed data....
foreach ($tableFromTemplate as $index => $setting) {
          echo $form->field($setting, "[$index]id_table")->label($setting->id_table);
}   ?>

And in this form, foreach without implementing the drop-down list through dropDownList, everything works, everything is saved, the [$index] key takes on values ​​and is passed to the dom as [0],[1]...

Controller

public function actionUpdate($id)
{
$model = $this->findModel($id);

$tableDesc =TagTable::find()->asArray()->all();//для массива имен таблиц

$idTemplate = $model->id;

$tableFromTemplate = TableFromTemplate::find()->where(['id_template' => $idTemplate])->indexBy('id')->all();//массив моделей для редактируемого шаблона

if (Model::loadMultiple($tableFromTemplate, Yii::$app->request->post()) && Model::validateMultiple($tableFromTemplate)) {
foreach ($tableFromTemplate as $setting) {
$setting->save(false);
}
return $this->redirect(['view', 'id' => $model->id]);
}

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
'category' => TemplateCategory::find()->all(),
'tableDesc' => $tableDesc,
'tableFromTemplate' => $tableFromTemplate
]);
}

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ettychel, 2020-04-17
@Fezas

Look at your quotes
Help from Google

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question