K
K
Konstantin Zhikhor2018-03-14 00:13:14
Yii
Konstantin Zhikhor, 2018-03-14 00:13:14

How to make tabular input in Yii in view create?

Hello, please tell me how to make a view for action create.
I don’t need to write links to sites and write everything is written there. I have seen all these links.
please just help and write the code.
actionCreate code

public function actionCreate()
    {
        $model = new Goals();
        
    $model->id_user = Yii::$app->user->id;
    $count = count(Yii::$app->request->post('Stage', []));
      $stages = [new Stage()];
      for($i = 1; $i < $count; $i++) {
        $stages[] = new Stage();
      }
        if ($model->load(Yii::$app->request->post())) {
      $model->doc = UploadedFile::getInstance($model, 'doc');
      if ($model->doc) {
        if($model->upload())
        {
          $model->doc = $model->doc->baseName.'.'.$model->doc->extension;
        }
      }
      
      if($model->save())
      {
        /* if($stages)
        {
          if (Model::loadMultiple($stages, Yii::$app->request->post()) && Model::validateMultiple($stages)) {
            foreach ($stages as $stg) {
              $stg->id_user = $model->id_user;
              $stg->goal_id = $model->id;
              $stg->save(false);
            }
          }
        } */
        
        return $this->redirect(['view', 'id' => $model->id]);
      }
            
        }

        return $this->render('create', [
            'model' => $model,
            'stages' => $stages,
        ]);
    }

layout
<div class="stage">
      <div class="ag_1_2"><?= $form->field($stages, '[]title')->textInput(['class'=>'input_goal','placeholder'=>'Название этапа'])->label(false) ?></div>
      
      <div class="ag_1_2"><?= $form->field($stages, '[]description')->textarea(['class'=>'input_goal','placeholder'=>'Описание этапа'])->label(false) ?></div>
        <ul class="ag_8">
          <li class="ag_l"><img src="img/icon/leveldown.svg" alt=""/></li>
          <li class="ag_ac"><?= $form->field($stages, '[]date_finish_stage')->widget(\yii\jui\DatePicker::className(),['class'=>'datepicker','options' => ['data' => ['picker' => 'datepicker']]])->label(false) ?></li>
        </ul>
                
        </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-03-14
@slo_nik

Goodnight.

if ($model->load(Yii::$app->request->post()) && Model::loadMultiple($stages, Yii::$app->request->post()) && Model::validateMultiple($stages, Yii::$app->request->post())) {
      if($model->save()){
          foreach($stages as $stage){
              $stage->save(false);
          }
          // продолжаете выполнять код, загружать файлы, делать redirect()
      }
 }

In the view, you create fields for the $stages model using the foreach() construct;
foreach($stages as $key => $stage){
    echo $form->field($stage, "[$key]title")->textInput();
    // остальные поля по аналогии.
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question