kidzen / yii2-dynamicform

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
Other
16 stars 17 forks source link

Error : Added More Field If Use dynamicform in Modal #26

Open biladina opened 3 years ago

biladina commented 3 years ago

hello, I have a problem with dynamicform, I use it in modal with ajax form.. everytime I used dynamic form and after I close the modal or submit the data, and when I open the modal again and use the dynamic form, dynamic form will add more field, and it repeated constantly..

Yii2 version : 2.0.42 Bootstrap : 4.6.0 AdminLTE3 : https://github.com/biladina/yii2-ajaxcrud-bs4 Yii2 AjaxCRUD : https://github.com/biladina/yii2-ajaxcrud-bs4

this is my view :

<?php
use yii\helpers\Html;
use yii\bootstrap4\ActiveForm;
use kidzen\dynamicform\DynamicFormWidget;
?>

<style type="text/css">
    .invalid-feedback { display: block; }
</style>

<div class="data-penilaian-form">

    <?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>

    <?= $form->field($model, 'penilaian')->textInput(['maxlength' => true]) ?>

    <?php DynamicFormWidget::begin([
        'widgetContainer' => 'dynamicform_wrapper',
        'widgetBody' => '.container-items',
        'widgetItem' => '.house-item',
        'min' => 1,
        'insertButton' => '.add-house',
        'deleteButton' => '.remove-house',
        'model' => $model_nilai[0],
        'formId' => 'dynamic-form',
        'formFields' => [
            'atribut',
            'nilai'
        ],
    ]); ?>
    <table class="table table-bordered table-striped">
        <thead>
            <tr>
                <th>Data</th>
                <th class="text-center" style="width: 90px;">
                    <button type="button" class="add-house btn btn-success btn-xs"><span class="fa fa-plus"></span></button>
                </th>
            </tr>
        </thead>
        <tbody class="container-items">
        <?php foreach ($model_nilai as $index => $model_nilai): ?>
            <tr class="house-item">
                <td class="vcenter">
                    <?php
                        // necessary for update action.
                        if (! $model_nilai->isNewRecord) {
                            echo Html::activeHiddenInput($model_nilai, "[{$index}]id");
                        }
                    ?>
                    <div class="form-row">
                        <div class="col p-2">
                            <?= $form->field($model_nilai, "[{$index}]atribut")->textInput() ?>
                        </div>
                        <div class="col p-2">
                            <?= $form->field($model_nilai, "[{$index}]nilai")->textInput() ?>
                        </div>
                    </div>
                </td>
                <td class="text-center vcenter" style="width: 90px; verti">
                    <button type="button" class="remove-house btn btn-danger btn-xs"><span class="fa fa-minus"></span></button>
                </td>
            </tr>
         <?php endforeach; ?>
        </tbody>
    </table>
    <?php DynamicFormWidget::end(); ?>

    <?php ActiveForm::end(); ?>
</div>

this is my controller :

public function actionCreate()
{
    $request = Yii::$app->request;
    $model = new DataPenilaian();

    $model_nilai = [new DataPenilaianNilai];

    if($request->isAjax)
    {
        /*
        *   Process for ajax request
        */
        Yii::$app->response->format = Response::FORMAT_JSON;
        if($request->isGet)
        {
            return [
                'title'=> Yii::t('yii2-ajaxcrud', 'Create New')." Data Penilaian",
                'content'=>$this->renderAjax('create', [
                    'model' => $model,
                    'model_nilai' => $model_nilai,
                ]),
                'footer'=> Html::button(Yii::t('yii2-ajaxcrud', 'Close'), ['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                    Html::button(Yii::t('yii2-ajaxcrud', 'Create'), ['class'=>'btn btn-primary','type'=>"submit"])
            ];         
        }
        else if($model->load($request->post()) )
        {
            return [
                'forceReload'=>'#crud-datatable-pjax',
                'title'=> Yii::t('yii2-ajaxcrud', 'Create New')." Data Penilaian",
                'content'=>'<span class="text-success">'.Yii::t('yii2-ajaxcrud', 'Create').' Data Penilaian '.Yii::t('yii2-ajaxcrud', 'Success').'</span>',
                'footer'=> Html::button(Yii::t('yii2-ajaxcrud', 'Close'), ['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                    Html::a(Yii::t('yii2-ajaxcrud', 'Create More'), ['create'],['class'=>'btn btn-primary','role'=>'modal-remote'])
            ];
        }
        else
        {
            return [
                'title'=> Yii::t('yii2-ajaxcrud', 'Create New')." Data Penilaian",
                'content'=>$this->renderAjax('create', [
                    'model' => $model,
                    'model_nilai' => $model_nilai,
                ]),
                'footer'=> Html::button(Yii::t('yii2-ajaxcrud', 'Close'), ['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]).
                    Html::button(Yii::t('yii2-ajaxcrud', 'Save'), ['class'=>'btn btn-primary','type'=>"submit"])
            ];
        }
    }
}

I also have video when this error happen

https://user-images.githubusercontent.com/10651085/129474259-06db3ded-9adb-4bc2-ab66-2568429767f9.mp4