亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Yii2:從列表視圖獲取數據以使用活動記錄進行記錄

Yii2:從列表視圖獲取數據以使用活動記錄進行記錄

隔江千里 2023-07-20 15:50:06
listview我正在創建一份調查問卷,并嘗試記錄獲得的答案,我在 a 中顯示從一個表(問題)獲得的問題,并且我想使用 將它們記錄在另一個表(答案)中active record。我嘗試在 itemView 類中添加活動表單,但每次我有超過 1 個問題時,發送按鈕都會出現重復,然后我嘗試將其添加到 itemView 之外,如果提交按鈕僅出現一次但我無法獲取它是 itemView 中列出的數據,因為我不知道如何發送活動表單的字段以從 itamView 獲取數據,我嘗試通過 itemView 的渲染發送它們,但它向我拋出了未定義的變量錯誤??捶?lt;?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以便能夠將它們注冊到答案表中。
查看完整描述

2 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

您收到未定義變量錯誤,因為您沒有將變量$form從主視圖傳遞到_answers.php. 你可以這樣傳遞:


<?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) use ($form) {

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

            'form' => $form,

            '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']) ?>

至于如何發送帶有問題 id 的多個答案,您可以使用vvpanchev 的答案中提到的方式或添加帶有問題 id 的隱藏字段。帶有隱藏字段的視圖_answers.php:


<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

            //set the question's id to answer model if you haven't done that already

            $answer->question_id = $model->id;

            //output the hidden input with question id

            echo \yii\helpers\Html::activeHiddenInput($answer, "[$index]question_id"); 

        ?>

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

    </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++) {

    $answers[] = new Answer();

}

if (

    \yii\base\Model::loadMultiple($answers, \Yii::$app->request->post())

    && \yii\base\Model::validateMultiple($answers)

) {

    // ... save your answers and/or do other things needed

}


查看完整回答
反對 回復 2023-07-20
?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

像這樣更改您的答案輸入,您將獲得每個問題的答案輸入:


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

當您提交表格時,它將連同所有問題的答案一起提交。所以你可以像這樣檢查并保存$_POST:


if(isset($_POST['Answer']) and !empty($_POST['Answer'])){

    foreach($_POST['Answer'] as $question_id => $answer){

        //save your answer to your question

    }

}

你也必須像這樣改變你的ajaxvalidation foreach


查看完整回答
反對 回復 2023-07-20
  • 2 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號