以 yii2-dynamic 形式更新时获取重复的文本值

Getting duplicate text values while updating in yii2-dynamic form

我正在使用 wbraganca / yii2-动态形式。我创建了一个动态表单,能够查看数据。现在,当我尝试更新它时,每条记录的第一个文本框的值始终相同。以下是我的看法

此处的 Slab 名称为 S-1S-4。现在,当我尝试更新它们时

上面所有的slab名称都是S-1,其他字段的数据都是正确的

 public function actionUpdate($id)
{
    $model = $this->findModel($id);

    $modelsTariffSlabs = $this->findModelSlabs($model->id);

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

        $oldIDs = ArrayHelper::map((array)$modelsTariffSlabs, 'id', 'id');
        $modelsTariffSlabs = CustomtariffModel::createMultiple(MdcTariffSlabs::classname(), $modelsTariffSlabs);
        CustomtariffModel::loadMultiple($modelsTariffSlabs, Yii::$app->request->post());
        $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsTariffSlabs, 'id', 'id')));

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

        if ($valid) {
            $transaction = \Yii::$app->db->beginTransaction();
            try {
                if ($flag = $model->save(false)) {
                    if (! empty($deletedIDs)) {
                        MdcTariffSlabs::deleteAll(['id' => $deletedIDs]);
                    }
                    foreach ($modelsTariffSlabs as $modelTariffSlabs) {
                        $modelTariffSlabs->t_id = $model->id;
                        if (! ($flag = $modelTariffSlabs->save(false))) {
                            $transaction->rollBack();
                            break;
                        }
                    }
                }
                if ($flag) {
                    $transaction->commit();
                    return $this->redirect(['view', 'id' => $model->id]);
                }
            } catch (Exception $e) {
                $transaction->rollBack();
            }
        }


        return $this->redirect(['view', 'id' => $model->id]);
    }

    return $this->render('update', [
        'model' => $model,
        'modelsTariffSlabs' => (empty($modelsTariffSlabs)) ? [new MdcTariffSlabs()] : $modelsTariffSlabs
    ]);
}

protected function findModelSlabs($id)
{
    if (($model = MdcTariffSlabs::find()->where(['t_id'=>$id])->all()) !== null) {

        return $model;
    }

    throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}

_form

<div class="container-items"><!-- widgetContainer -->
                <?php foreach ($modelsTariffSlabs as $i => $modelTariffSlabs): ?>
                    <div class="item panel panel-default"><!-- widgetBody -->
                        <div class="panel-heading">
                            <h3 class="panel-title pull-left">Slab</h3>
                            <div class="pull-right">
                                <button type="button" id="addBtn" class="js-add-filter btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
                                <button type="button" id="remBtn" class="js-remove-filter btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                        <div class="panel-body">
                            <?php
                            // necessary for update action.
                            if (! $modelTariffSlabs->isNewRecord) {
                                echo Html::activeHiddenInput($modelTariffSlabs, "[{$i}]id");
                            }
                            ?>
                            <div class="row">
                                <div class="col-sm-3">
                                    <?= $form->field($modelTariffSlabs, "[{$i}]slab_name")->textInput(['readonly'=>true,'value'=>'S-1','maxlength' => 10,"class"=>"form-control js-slab-name"]) ?>
                                </div>
                                <div class="col-sm-3">
                                    <?= $form->field($modelTariffSlabs, "[{$i}]slab_start")->textInput(['maxlength' => 10,"class"=>"form-control js-slab-start"]) ?>
                                </div>
                                <div class="col-sm-3">
                                        <?= $form->field($modelTariffSlabs, "[{$i}]slab_end")->textInput(['maxlength' => 10,"class"=>"form-control js-slab-end"]) ?>
                                    </div>
                                <div class="col-sm-3">
                                    <?= $form->field($modelTariffSlabs, "[{$i}]rate")->textInput(['maxlength' => 10, "class"=>"form-control js-slab-rate"]) ?>
                                </div>
                            </div><!-- .row -->

                        </div>
                    </div>
                <?php endforeach; ?>
            </div>

如何显示实际的 slab 名称?

您必须检查 slab_name。这是截图

<div class="col-sm-3">
<?php if ($modelTariffSlabs->isNewRecord) { ?>
<?= $form->field($modelTariffSlabs, "[{$i}]slab_name")->textInput(['readonly'=>true,'value'=>'S-1','maxlength' => 10,"class"=>"form-control js-slab-name"]) ?>

<?php } elseif (!$modelTariffSlabs->isNewRecord) {?>

<?=$form->field($modelTariffSlabs, "[{$i}]slab_name")->textInput(['readonly' => true, 'value' => $modelTariffSlabs->slab_name, 'maxlength' => 10, "class" => "form-control js-slab-name"])?>

<?php }?>
</div>