[英]How to show form fields while updating a record in Yii2
I am using wbraganca / yii2-dynamicform .我正在使用wbraganca / yii2-dynamicform 。 I am able to create a new record.我能够创建一个新记录。 There are two models that I am using to create records.我使用两种模型来创建记录。 The data is saved into both of the tables.数据保存在两个表中。
<?= $form->field($model, 't_name')->textInput(['maxlength' => true]) ?>
<div class="row">
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-flash"></i> Tariff Slabs</h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 10, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.js-add-filter', // css class
'deleteButton' => '.js-remove-filter', // css class
'model' => $modelsTariffSlabs[0],
'formId' => 'dynamic-form',
'formFields' => [
'slab_name',
'slab_start',
'slab_end',
'rate'
],
]); ?>
<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(['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]) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
Now when I try to update it I am not data of the second model.现在,当我尝试更新它时,我不是第二个 model 的数据。
Update.php更新.php
<div class="mdc-tariff-update">
<?= $this->render('_form', [
'model' => $model,
'modelsTariffSlabs' => $modelsTariffSlabs
]) ?>
</div>
Update controller更新 controller
public function actionUpdate($id)
{
$model = $this->findModel($id);
$modelsTariffSlabs = [new MdcTariffSlabs()];
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$oldIDs = ArrayHelper::map($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
]);
}
How can I show the values of all fields in my update form?如何在更新表单中显示所有字段的值?
Any help would be highly appreaciated.任何帮助将不胜感激。
Or you can call relation like (when you are on update mode):或者您可以调用关系(当您处于更新模式时):
$modelsTariffSlabs = $model->mdcTariffSlabs
If mdcTariffSlabs
is name of relation - it returns array if related models.如果mdcTariffSlabs
是关系名称 - 如果相关模型返回数组。 Or if you don't have relation, you can create it in main model:或者如果你没有关系,你可以在主 model 中创建它:
public function getMdcTariffSlabs()
{
return $this->hasMany(MdcTariffSlabs::className(), ['t_id' => 'id']);
}
I assume that the issue is in this line我认为问题出在这一行
$modelsTariffSlabs = [new MdcTariffSlabs()];
It always create an empty model.它总是创建一个空的 model。 You have to get all the records that are saved.您必须获取所有已保存的记录。 Below code should work下面的代码应该可以工作
Assuming that $modelTariffSlabs->t_id = $model->id; // t_id is id of your main table
假设$modelTariffSlabs->t_id = $model->id; // t_id is id of your main table
$modelTariffSlabs->t_id = $model->id; // t_id is id of your main table
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.'));
}
And then change然后改变
$modelsTariffSlabs = [new MdcTariffSlabs()];
TO至
$modelsTariffSlabs = $this->findModelSlabs($model->id);
I hope this will help我希望这个能帮上忙
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.