Search code examples
yii2dynamicform

Yii2 dynamic form with number of record based on variable value


I cannot save all records but just the last.

In a form users insert Year_begin and Year_end

A variable $years (where $years = Year_end - Year_begin) is passed to the dynamic form.

Example:

  • Worked 2010 and 2011 I need to generate two records
  • Worked from 2010 to 2015 I need to generate six records

I don't want to use "+" and "-" button to generate new record.

Every record shows the correct YEAR value

This is the result: Form image with two self-generated record

This is the code of my form with while cycle:

<?php foreach ($modelsComm as $i => $modelComm): ?>
<div class="item panel panel-default"><!-- widgetBody -->

    <div class="panel-heading">
        <h3 class="panel-title pull-left">id_calcolo</h3>
        <div class="pull-right">
            <button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
            <button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
        </div>
        <div class="clearfix"></div>
    </div>
    <div class="panel-body">
    <!-- loop begin -->
        <?php
        $n=0;
        while ($n < $years) {
            // necessary for update action.
            if (! $modelComm->isNewRecord) {
                echo Html::activeHiddenInput($modelComm, "[{$i}]id");
            }
        ?>
        <div class="row">
            <div class="col-sm-2">
                <?= $form->field($modelComm, "[{$i}]year")->textInput(['value' => $year_begin+$n]) ?>
            </div>
            <div class="col-sm-4">
                <?= $form->field($modelComm, "[{$i}]worked")->textInput(['maxlength' => true]) ?>
            </div>
        <div class="col-sm-4">
                <?= $form->field($modelComm, "[{$i}]paid")->textInput(['maxlength' => true]) ?>
            </div>
        </div><!-- .row -->
        <?php
        $n++;
        }
        ?>   
        <!-- loop end -->  
    </div>

</div>
<?php endforeach; ?>

My Controller function:

public function actionCreate($years,$year_begin)
{
    $model = new CalcReference();
    $modelsComm = [new Comm];

    if ($model->load(Yii::$app->request->post()) && $model->save()) 
    {
        $modelsComm = Model::createMultiple(Comm::classname());
        Model::loadMultiple($modelsComm, Yii::$app->request->post());

        // ajax validation
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ArrayHelper::merge(
                ActiveForm::validateMultiple($modelsComm),
                ActiveForm::validate($model)
            );
        }

        // validate all models
        $valid = $model->validate();
        $valid = Model::validateMultiple($modelsComm) && $valid;

        if ($valid) {
            $transaction = \Yii::$app->db->beginTransaction();
            try {
                if ($flag = $model->save(false)) {
                    foreach ($modelsComm as $modelComm) {
                        $modelComm->id_pratica = $model->id;
                        if (! ($flag = $modelComm->save(false))) {
                            $transaction->rollBack();
                            break;
                        }
                    }
                }
                if ($flag) {
                    $transaction->commit();
                    return $this->redirect(['view',
                    'id' => $model->id,
                    'year_begin' => $yeear_begin,
                    'years' => $years,
                    ]);
                }
            } catch (Exception $e) {
                $transaction->rollBack();
            }
        }
      /*  return $this->redirect(['view', 'id' => $model->id,
            'anno_inizio' => $anno_inizio,
            'qta_anni' => $qta_anni,
            'dal' => $dal,
            'al' => $al,
            'id_pratica' => $id_pratica,
            ]);*/
    } else 
    {
        return $this->render('create', [
            'model' => $model,
            'modelsComm' => (empty($modelsComm)) ? [new Comm] : $modelsComm,
            'year_begin' => $yeear_begin,
            'years' => $years,
        ]);
    }
}

My model:

public static function tableName()
{
    return 'comm';
}


public function rules()
{
    return [
        [['year', 'worked', ], 'required'],
        [['worked', 'paid'], 'integer'],
    ];
}


public function attributeLabels()
{
    return [
        'id' => 'ID',
        'year' => 'Year',
        'worked' => 'Worked days',
        'paid' => 'Total Paid',
     ];
}

This is model Model.php:

class Model extends \yii\base\Model
{
/**
 * Creates and populates a set of models.
 *
 * @param string $modelClass
 * @param array $multipleModels
 * @return array
 */
public static function createMultiple($modelClass, $multipleModels = [])
{
    $model    = new $modelClass;
    $formName = $model->formName();
    $post     = Yii::$app->request->post($formName);
    $models   = [];

    if (! empty($multipleModels)) {
        $keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));
        $multipleModels = array_combine($keys, $multipleModels);
    }

    if ($post && is_array($post)) {
        foreach ($post as $i => $item) {
            if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {
                $models[] = $multipleModels[$item['id']];
            } else {
                $models[] = new $modelClass;
            }
        }
    }

    unset($model, $formName, $post);

    return $models;

Solution

  • In the years loop You use $n but input names have $i inside. So all Your inputs are initialized with [0] prefix. That's why on server side You have only the last record.

    Just change [{$i}]id to [{$n}]id, [{$i}]year to [{$n}]year and so on