0

I have a problem when it comes to saving the fields of tables being displayed three times. Cant save the unique value being saved in the text fields. Kindly someone direct me to the right answer please.

View code :

<h2>List of Documents</h2>
<table class="table">
    <?php foreach($formlist as $item) { ?>
    <tr>
        <td><?= $form->field($model, '['.$item->id.']value')->radioList(['yes'=>' yes','no'=>' no'])->label($item['title']); ?></td>
    </tr>
    <?php } ?>
</table>

Controller code :

public function actionCreate()
{
    $model = new Form();
    $forminfo = new Forminfo();
    $forminfo->id = $model->forminfo_id;

    /*$sql = 'SELECT * FROM formlist ORDER BY id ASC';
    $db = Yii::$app->db;
    $formlist = $db->createCommand($sql)->queryAll();*/
    // same of ->

    $formlist = Formlist::find()->orderBy(['id'=>SORT_ASC])->all();

    if ($forminfo->load(Yii::$app->request->post()) && $model->load(Yii::$app->request->post())) {

         $forminfo->save(false); // skip validation as model is already validated
         $model->forminfo_id =  $forminfo->id; // no need for validation rule on user_id as you set it yourself
         $model->save(false); 

         Yii::$app->getSession()->setFlash('success', 'You have successfully saved your data.');
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
            'forminfo' => $forminfo,
            'formlist' => $formlist,
        ]);
    }
}
soju
  • 25,111
  • 3
  • 68
  • 70
Mang Jose
  • 3
  • 2
  • 1
    show the related code .. view and action – ScaisEdge May 02 '16 at 05:57
  • I already made a changes and display the code for view. For action hmm.. still have no idea how to work on it.... – Mang Jose May 02 '16 at 06:52
  • You don't have an action? how do you show this view and manage the submitted value? .. and in this view i don't see the code relate to form and submit .. please add it.. – ScaisEdge May 02 '16 at 06:57
  • Hi I already displayed the action you are asking a while ago. Please do refer on the code provided in the top sir... Thanks a lot in advance... – Mang Jose May 02 '16 at 07:37
  • Explain better the problem is related to the $formlist (radiolist) ? you have problem saving these values? – ScaisEdge May 02 '16 at 08:13
  • yes i have problem on how to save those fields with array.... – Mang Jose May 02 '16 at 08:50

1 Answers1

0

For accessing tabular input you should use loadMultiple('yourModel')

or a loop on

 $post= Yii::$app->request->post());

 foreach ($post['your_model'] as $key => $myModel) {

   //  $myModel contain the current model 
 }

this yii2 guide could be useful http://www.yiiframework.com/doc-2.0/guide-input-tabular-input.html

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107