I am trying to make a form which includes several instances on the same model.
In my case the model is PollResult that is one user’s answers to a Poll (a question).
I use an indexed array of PollResults and try to use it in my form but this results in the error Call to a member function formName() on array.
The error occurs at the first field’s declaration line of the form and probably at the other fields.
Here is my controller’s code
class SurveyController extends Controller {
public function actionAnswer($id) { $user_id=$username = Yii::$app->user->identity->id; $model=new Survey(); $polls=Poll::find()->where(['survey_id'=>$id])->all(); $count=Poll::find()->where(['survey_id'=>$id])->count(); echo('count is : '.$count); $pollResults=[]; foreach ($polls as $poll) { $pollResult=PollResult::find()->where(['poll_id'=>$poll->id])->one(); if(!$pollResult){ $pollResult=new PollResult(); $pollResult->poll_id=$poll->id; $pollResult->user_id=$user_id; } $pollResults['nb'.$poll->id]=$pollResult;//on utilise le poll_id pour identifier les pollResults dans le formulaire } if (Model::loadMultiple($pollResults, Yii::$app->request->post()) && Model::validateMultiple($pollResults)) { foreach ($pollResults as $pollResult) { $pollResult->save(); } return $this->redirect(['answer','id'=>$id]); } return $this->render('answer',[ 'model'=>$model, 'pollResults' => $pollResults ]); }
Then answer.php
<?= $this->render('_form', ['model' => $model, 'pollResults'=>$pollResults]); ?>
Then the form itself
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use app\modules\admin\models\Poll; use app\modules\admin\models\Answer; ?><?php $form = ActiveForm::begin([ 'enableClientValidation' => false, ]); ?> <fieldset> <?php foreach($pollResults as $key => $pollResult){ //on crée une array associative clé = id de la réponse, valeur = texte de la réponse — ceci pour les questions à choix multiples $answers=Answer::find()->where(['poll_id'=>$pollResults[$key]->poll_id])->all(); $poll=Poll::find()->where(['id'=>$pollResult->poll_id])->one(); echo $form->field($pollResults,'[$key]user_id')->hiddenInput(['value' => Yii::$app->user->identity->id])->label(false); echo $form->field($pollResults,'[$key]poll_id')->hiddenInput(['value' => $poll->id])->label(false); $selected=$pollResults[$key]->answer_ids; $pollResults[$key]->answer_ids=$selected; $items=[]; foreach($answers as $ans){ $items[$ans->id]=$ans->answer_text; } switch ($poll->allow_multiple) {//0 multiple answer in list, 1 only one answer, 2 textual answer case 0: echo $form->field($pollResults,'[$key]answer_ids')->checkboxList($items,['separator' => '<br>'])->label(false); break; case 1: echo $form->field($pollResults,'[$key]answer_ids')->RadioList($items,['separator' => '<br>'])->label(false); break; case 2: echo $form->field($pollResults,'[$key]answer_ids')->textarea(['rows' => 3])->label(' Saisissez votre réponse ici'); break; } echo('<hr/>'); } ?> </fieldset> <?= Html::submitButton('Save'); ?> <?php ActiveForm::end(); ?>
What is the matter ? What can I do to solve this ?