[英]Update with Yii2-dynamicforms (Nested dynamic forms)
In my Yii 2 project, I have a Nested dynamic form working with yii2-dynamicform extension. 在我的Yii 2项目中,我有一个使用yii2-dynamicform扩展的嵌套动态表单。 I could make it work following the wiki guide , but I had to ask for help in this StackOverflow question to make it work properly.
我可以按照wiki指南进行操作 ,但我不得不在StackOverflow问题中寻求帮助以使其正常工作。 But I'm having a little problem on Update, when I try to add more fields.
但是当我尝试添加更多字段时,我在Update上遇到了一些问题。
Before explain, let me show my scenario: 在解释之前,让我展示一下我的场景:
Updating a record, when I click to Add a new Slot field, instead of adding just one, it adds the number of groups that I have already added. 更新记录时,当我单击添加新的插槽字段而不是仅添加一个时,它会添加我已添加的组的数量。
For example: In the image above I have two groups (group1 and group2). 例如:在上图中,我有两个组(group1和group2)。 If i click to add one slot, it adds two (number of groups) slots at once.
如果我单击添加一个插槽,它会一次添加两个(组的数量)插槽。
Anyone can help me, please? 有人可以帮帮我吗?
public function actionUpdate($id)
{
$model = $this->findModel($id);
$oldGrupoIds = MissaoGrupo::find()->select('id')->where(['missao_id' => $id])->asArray()->all();
$oldGrupoIds = ArrayHelper::getColumn($oldGrupoIds, 'id');
$modelsGrupos = MissaoGrupo::findAll(['id' => $oldGrupoIds]);
$modelsGrupos = (empty($modelsGrupos)) ? [new MissaoGrupo] : $modelsGrupos;
$oldSlotIds = [];
foreach ($modelsGrupos as $i => $modelGrupo) {
$oldSlots = MissaoSlot::findAll(['grupo_id' => $modelGrupo->id]);
$modelsSlots[$i] = $oldSlots;
$oldSlotIds = array_merge($oldSlotIds, ArrayHelper::getColumn($oldSlots, 'id'));
$modelsSlots[$i] = (empty($modelsSlots[$i])) ? [new MissaoSlot] : $modelsSlots[$i];
}
if ($model->load(Yii::$app->request->post())) {
$modelsGrupos = Model::createMultiple(MissaoGrupo::classname(), $modelsGrupos);
Model::loadMultiple($modelsGrupos, Yii::$app->request->post());
$newGrupoIds = ArrayHelper::getColumn($modelsGrupos, 'id');
$newSlotIds = [];
$loadsData['_csrf'] = Yii::$app->request->post()['_csrf'];
$i = 0;
foreach ($modelsGrupos as $id => $value) {
$loadsData['MissaoSlot'] = Yii::$app->request->post()['MissaoSlot'][$i];
if (!isset($modelsSlots[$id])) {
$modelsSlots[$id] = [new MissaoSlot];
}
$modelsSlots[$id] = Model::createMultiple(MissaoSlot::classname(), $modelsSlots[$id], $loadsData);
Model::loadMultiple($modelsSlots[$id], $loadsData);
$newSlotIds = array_merge($newSlotIds, ArrayHelper::getColumn($loadsData['MissaoSlot'], 'id'));
$i++;
}
$delSlotIds = array_diff($oldSlotIds, $newSlotIds);
if (! empty($delSlotIds)) MissaoSlot::deleteAll(['id' => $delSlotIds]);
$delGrupoIds = array_diff($oldGrupoIds, $newGrupoIds);
if (! empty($delGrupoIds)) MissaoGrupo::deleteAll(['id' => $delGrupoIds]);
$valid = $model->validate();
$valid = $this->validaMissao($modelsGrupos, $modelsSlots) && $valid;
if ($valid) {
if ($this->saveMissao($model, $modelsGrupos, $modelsSlots)) {
return $this->redirect(['view', 'id' => $model->id]);
}
}
}
return $this->render('update', [
'model' => $model,
'modelsGrupos' => $modelsGrupos,
'modelsSlots' => $modelsSlots,
]);
}
protected function saveMissao($modelMissao, $modelsGrupos, $modelsSlots)
{
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($go = $modelMissao->save(false)) {
foreach ($modelsGrupos as $i => $modelGrupo) {
// Salva os Grupos
$modelGrupo->missao_id = $modelMissao->id;
if ($go = $modelGrupo->save(false)) {
foreach ($modelsSlots[$i] as $j => $modelSlot) {
// Salva os Slots
$modelSlot->grupo_id = $modelGrupo->id;
if (! ($go = $modelSlot->save(false))) {
$transaction->rollBack();
break;
}
}
}
}
}
if ($go) {
$transaction->commit();
}
} catch (Exception $e) {
$transaction->rollBack();
}
return $go;
}
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
/* @var $this yii\web\View */
/* @var $model app\models\Missao */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="missao-form">
<?php $form = ActiveForm::begin(['id' => 'frm_missao']); ?>
<?= $form->errorSummary($model) ?>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">
<a href="#aba-detalhes-missao" aria-controls="home" role="tab" data-toggle="tab"><i class="fa fa-info-circle"></i> Detalhes da missão</a>
</li>
<li role="presentation">
<a href="#aba-slots" aria-controls="profile" role="tab" data-toggle="tab"><i class="fa fa-users"></i> Slots</a>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="aba-detalhes-missao">
<br/>
<?= $form->field($model, 'nome')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'desc')->textInput(['maxlength' => true]) ?>
</div>
<div role="tabpanel" class="tab-pane" id="aba-slots">
<br/>
<div class="panel panel-default">
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper',
'widgetBody' => '.container-grupos',
'widgetItem' => '.grupo',
//'limit' => 4,
'min' => 1,
'insertButton' => '.add-grupo',
'deleteButton' => '.remove-grupo',
'model' => $modelsGrupos[0],
'formId' => 'frm_missao',
'formFields' => [
'nome',
],
]); ?>
<div class="panel panel-default">
<div class="panel-heading">Grupos / Slots</div>
<div class="panel-body container-grupos">
<?php foreach ($modelsGrupos as $i => $modelGrupo): ?>
<div class="row grupo"> <!-- Item Grupo - INICIO -->
<table class="table">
<tr>
<th style="width:5%">#</th>
<th>Grupo</th>
<th>Slot</th>
</tr>
<tr>
<td><button class="btn btn-danger remove-grupo"><i class="fa fa-minus"></i></button></td>
<td class="col-md-4">
<?php
// necessary for update action.
if (! $modelGrupo->isNewRecord) {
echo Html::activeHiddenInput($modelGrupo, "[{$i}]id");
}
?>
<?= $form->field($modelGrupo, "[{$i}]nome")->textInput(['maxlength' => true, 'placeholder' => 'Nome do Grupo'])->label(false) ?>
</td>
<td><!-- Slots -->
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_inner',
'widgetBody' => '.container-slots',
'widgetItem' => '.slot',
//'limit' => 10,
'min' => 1,
'insertButton' => '.add-slot',
'deleteButton' => '.remove-slot',
'model' => $modelsSlots[$i][0],
'formId' => 'frm_missao',
'formFields' => [
'nome',
],
]); ?>
<table class="table container-slots">
<?php foreach ($modelsSlots[$i] as $j => $modelSlot): ?>
<tr class="slot">
<td>
<?php
// necessary for update action.
if (! $modelSlot->isNewRecord) {
echo Html::activeHiddenInput($modelSlot, "[{$i}][{$j}]id");
}
?>
<?= $form->field($modelSlot, "[{$i}][{$j}]nome")->textInput(['maxlength' => true, 'placeholder' => 'Nome do Slot'])->label(false) ?>
</td>
<td style="width:5%"><button class="btn btn-danger btn-xs remove-slot"><i class="fa fa-minus"></i></button></td>
</tr>
<?php endforeach; ?>
<tfoot>
<tr>
<td colspan="2"><button class="btn btn-info btn-xs add-slot"><i class="fa fa-plus"></i> Novo Slot</button></td>
</tr>
</tfoot>
</table>
<?php DynamicFormWidget::end(); ?>
</td><!--Slots - fim -->
</tr>
</table>
</div> <!-- Item Grupo - FIM -->
<?php endforeach; ?>
</div>
<div class="panel-footer">
<button class="btn btn-primary btn-xs add-grupo"><i class="fa fa-plus"></i> Novo Grupo</button>
</div>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Cadastrar' : 'Atualizar', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Problem solved! 问题解决了!
The author of Yii 2 Dynamic Forms extension don't updated the Packagist code (see issue #91 ). Yii 2 Dynamic Forms扩展的作者不更新Packagist代码(请参阅问题#91 )。
Replacing with GitHub code work properly! 用GitHub代码替换正常工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.