Yii2:从列表视图获取数据以使用活动记录进行记录

listview我正在创建一份调查问卷,并尝试记录获得的答案,我在 a 中显示从一个表(问题)获得的问题,并且我想使用 将它们记录在另一个表(答案)中active record。我尝试在 itemView 类中添加活动表单,但每次我有超过 1 个问题时,发送按钮都会出现重复,然后我尝试将其添加到 itemView 之外,如果提交按钮仅出现一次但我无法获取它是 itemView 中列出的数据,因为我不知道如何发送活动表单的字段以从 itamView 获取数据,我尝试通过 itemView 的渲染发送它们,但它向我抛出了未定义的变量错误。


看法


<?php $form = ActiveForm::begin([

'enableClientValidation' => false,

'enableAjaxValidation' => true,]) ?>


<?= ListView::widget([

    'layout' => '<div class="pull-left">{items}</div>',

    'dataProvider' => $dataProvider,

    'itemView' => function ($model, $key, $index, $widget) {

        return $this->render('_answers',[

            'model' => $model, 

            'index' => $index

        ]);

    },

]); ?><div class="form-group">

<?php echo Html::submitButton('<span class="fa fa-plus"></span>'.' '.Yii::t('backend', 'Send') , ['class' => 'btn btn-primary']) ?>

查看_answers


<td width="5%" class="vcenter" rowspan="3">

    <span class="panel-title-address"><label class="control-label">Nr: <?php echo ($index+1); ?></label></span>

</td>

<td width="95%" class="vcenter">

        <div class="form-group field-qquestion-0-title required">

            <label class="control-label" for="qquestion-type_id"><?= Yii::t('backend', 'Question'.' : ')?></label>

        </div>  

        <div class="form-group field-qquestion-0-title required">

            <label class="control-label" for="qquestion-type_id"><?= $model->question ?></label>

        </div>

    <div class="col-md-4">

        <?php echo $form->field($answer, 'answer')->textInput(['maxlength' => true]) ?>

    </div>

</td>

我想要获得的是每个问题的id和answer以便能够将它们注册到答案表中。


隔江千里
浏览 110回答 2
2回答

弑天下

您收到未定义变量错误,因为您没有将变量$form从主视图传递到_answers.php. 你可以这样传递:<?php $form = ActiveForm::begin(['enableClientValidation' => false,'enableAjaxValidation' => true,]) ?><?= ListView::widget([&nbsp; &nbsp; 'layout' => '<div class="pull-left">{items}</div>',&nbsp; &nbsp; 'dataProvider' => $dataProvider,&nbsp; &nbsp; 'itemView' => function ($model, $key, $index, $widget) use ($form) {&nbsp; &nbsp; &nbsp; &nbsp; return $this->render('_answers',[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'form' => $form,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'model' => $model,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'index' => $index,&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; },]); ?><div class="form-group"><?php echo Html::submitButton('<span class="fa fa-plus"></span>'.' '.Yii::t('backend', 'Send') , ['class' => 'btn btn-primary']) ?>至于如何发送带有问题 id 的多个答案,您可以使用vvpanchev 的答案中提到的方式或添加带有问题 id 的隐藏字段。带有隐藏字段的视图_answers.php:<td width="5%" class="vcenter" rowspan="3">&nbsp; &nbsp; <span class="panel-title-address"><label class="control-label">Nr: <?php echo ($index+1); ?></label></span></td><td width="95%" class="vcenter">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group field-qquestion-0-title required">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label class="control-label" for="qquestion-type_id"><?= Yii::t('backend', 'Question'.' : ')?></label>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group field-qquestion-0-title required">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label class="control-label" for="qquestion-type_id"><?= $model->question ?></label>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="col-md-4">&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //set the question's id to answer model if you haven't done that already&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $answer->question_id = $model->id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //output the hidden input with question id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo \yii\helpers\Html::activeHiddenInput($answer, "[$index]question_id");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <?php echo $form->field($answer, "[$index]answer")->textInput(['maxlength' => true]) ?>&nbsp; &nbsp; </div></td>如果您使用隐藏字段方法,那么在控制器中您可以使用\yii\base\Model::loadMultiple()方法将数据加载到答案模型中。您还可以用于\yii\base\Model::validateMultiple()验证。我假设答案模型类的名称是Answer.$count = count(\Yii::$app->request->post('Answer', []));$answers = [];for ($i = 0; $i < $count; $i++) {&nbsp; &nbsp; $answers[] = new Answer();}if (&nbsp; &nbsp; \yii\base\Model::loadMultiple($answers, \Yii::$app->request->post())&nbsp; &nbsp; && \yii\base\Model::validateMultiple($answers)) {&nbsp; &nbsp; // ... save your answers and/or do other things needed}

狐的传说

像这样更改您的答案输入,您将获得每个问题的答案输入:<?php echo $form->field($answer, '['.$model->id.']answer')->textInput(['maxlength' => true]) ?>当您提交表格时,它将连同所有问题的答案一起提交。所以你可以像这样检查并保存$_POST:if(isset($_POST['Answer']) and !empty($_POST['Answer'])){&nbsp; &nbsp; foreach($_POST['Answer'] as $question_id => $answer){&nbsp; &nbsp; &nbsp; &nbsp; //save your answer to your question&nbsp; &nbsp; }}你也必须像这样改变你的ajaxvalidation foreach
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript