Yii Framework Forum: wbraganca/yii2-dynamicform - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

wbraganca/yii2-dynamicform Rate Topic: -----

#1 User is offline   epulgaron 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 168
  • Joined: 19-February 14

Posted 05 November 2015 - 10:07 AM

hi forum,does someone has used this extension and its work correctly?
0

#2 User is offline   ilaiya 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 120
  • Joined: 09-October 14
  • Location:Bangalore, India

Posted 05 November 2015 - 10:38 PM

View Postepulgaron, on 05 November 2015 - 10:07 AM, said:

hi forum,does someone has used this extension and its work correctly?

Yes, I used, but for my requirements it works without any error.
0

#3 User is offline   epulgaron 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 168
  • Joined: 19-February 14

Posted 06 November 2015 - 09:22 AM

well i've got a few issues with it, first, when i create more than one register in the Db only store the last one, second, when i try to use any DataPicker in my form(panel)or select2 doesn't work, the select2 stay loading meanwhile the DataPicker dosen't work , could you help me? sorry for my bad english

View Postilaiya, on 05 November 2015 - 10:38 PM, said:

Yes, I used, but for my requirements it works without any error.

0

#4 User is offline   fsnxdi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 20-July 15
  • Location:Gdynia

Posted 06 November 2015 - 09:33 AM

View Postepulgaron, on 06 November 2015 - 09:22 AM, said:

well i've got a few issues with it, first, when i create more than one register in the Db only store the last one, second, when i try to use any DataPicker in my form(panel)or select2 doesn't work, the select2 stay loading meanwhile the DataPicker dosen't work , could you help me? sorry for my bad english


i didn`t used it, but:
remember that when you add multiple records to your with for example foreach you have to do:
$modelOfTableUWantToAddReccord->isNewRecord = true;

0

#5 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,665
  • Joined: 10-October 10
  • Location:Denmark

Posted 06 November 2015 - 09:48 AM

/* Moved to Extensions */
"Less noise - more signal"
0

#6 User is offline   epulgaron 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 168
  • Joined: 19-February 14

Posted 06 November 2015 - 10:14 AM

i did that, look here is my _form and my controller, i have 5 dynamic form in the same _form, take a look and if you can bring me some light
<?php

use yii\helpers\Html;
use wbraganca\dynamicform\DynamicFormWidget;
use backend\models\Model;
use backend\models\Modelo01;
use backend\models\Modelo03;
use backend\models\Modelo04;
use backend\models\Modelo05;
use backend\models\Implicados;
use yii\helpers\ArrayHelper;
use kartik\widgets\Select2;
use yii\helpers\Url;
use yii\bootstrap\ActiveForm;
use kartik\widgets\DatePicker;

/* @var $this yii\web\View */
/* @var $model backend\models\Planificacion */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="planificacion-form">

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

    <?= $form->field($model, 'plan_cod')->textInput(['maxlength' => 255,'style'=>'width:250px;']) ?>

    <?= $form->field($model, 'plan_desc')->textarea(['maxlength' => 5000,'style'=>'width:250px;']) ?>

    <?= $form->field($model, 'plan_acc_extra')->checkbox(array('value' => 'Si', 'uncheck' => 'No')) ?>

    <?= $form->field($model, 'orga_fk')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(\backend\models\Norganismos::find()->asArray()->all(), 'id_orga', 'orga_siglas'),
        'options' => ['placeholder' => 'Seleccione un organismo','style'=>'width:250px;',
            'onchange'=>'
                        $.get( "'.Url::toRoute('/planificacion/lists').'", { id: $(this).val() } )
                            .done(function( data ) {
                                $( "#'.Html::getInputId($model, 'entidad_fk').'" ).html( data );
                            }
                        );
                    '],
        'pluginOptions' => [
        'allowClear' => true
        ],
        ]); ?>

    <?= $form->field($model, 'entidad_fk')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(\backend\models\Entidad::find()->asArray()->all(), 'id_entidad', 'entidad_nombre'),
        'options' => ['placeholder' => 'Seleccione una entidad','style'=>'width:250px;'],
        'pluginOptions' => [
        'allowClear' => true
        ],
        ]); ?>

    <?= $form->field($model, 'tipo_accion_fk')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(\backend\models\Ntipoaccion::find()->asArray()->all(), 'id_tipo_accion', 'tipo_acc_desc'),
        'options' => ['placeholder' => 'Seleccione un tipo de acción ','style'=>'width:250px;'],
        'pluginOptions' => [
        'allowClear' => true
        ],
        ]); ?>

    <?= $form->field($model, 'particularidades_fk')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(\backend\models\Nparticularidades::find()->asArray()->all(), 'id_particularidades', 'particularidades_desc'),
        'options' => ['placeholder' => 'Seleccione una particularidad ','style'=>'width:250px;'],
        'pluginOptions' => [
        'allowClear' => true
        ],
        ]); ?>

    <?= $form->field($model, 'plan_fecha_inicio')->widget(DatePicker::classname(), [
    'model' => $model,
    'attribute' => 'plan_fecha_inicio',
    'options'=>['placeholder'=>'Inserte una fecha','style'=>'width:250px;'],
    'language' => 'es',
    'pluginOptions' => [
    'dateFormat' => 'yy-mm-dd',
     ],
    ]); ?>

    <?= $form->field($model, 'plan_fecha_fin')->widget(DatePicker::classname(), [
    'model' => $model,
    'attribute' => 'plan_fecha_fin',
    'options'=>['placeholder'=>'Inserte una fecha','style'=>'width:250px;'],
    'language' => 'es',
    'pluginOptions' => [
    'dateFormat' => 'yy-mm-dd',
     ],
    ]); ?>

    <?= $form->field($model, 'plan_cant_aut')->textInput(['style'=>'width:250px;']) ?>

    <?= $form->field($model, 'plan_cant_dias')->textInput(['style'=>'width:250px;']) ?>

    <?= $form->field($model, 'conclusion_fk')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(\backend\models\Nconclusiones::find()->asArray()->all(), 'id_conclusion', 'conclusion_desc'),
        'options' => ['placeholder' => 'Seleccione un estado ','style'=>'width:250px;'],
        'pluginOptions' => [
        'allowClear' => true
        ],
        ]); ?>

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

    <?= $form->field($model, 'plan_reg_por')->textInput(['maxlength' => true]) ?>
    
    
  
  <div class="row">
        <div class="panel panel-default">
        <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Modelo 01</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' => 1, // the maximum times, an element can be cloned (default 999)
                'min' => 0, // 0 or 1 (default 1)
                'insertButton' => '.add-item', // css class
                'deleteButton' => '.remove-item', // css class
                'model' => $modelsModelo01[0],
                'formId' => 'dynamic-form',
                'formFields' => [
                    'plan_orden',
                    'modelo01_directiva',
                    'modelo01_fecha_ini_real',
                    'modelo01_fecha_fin_real',
                    'calificacion_fk',
                    'modelo01_cant_aud_real',
                    'modelo01_dias_real',
                    'modelo01_phc',
                    'modelo01_phd',
                    'dic_fk',                    
                ],
            ]); ?>

            <div class="panel panel-default">
        <div class="panel-heading">
            <h4>
                <i class="glyphicon glyphicon-envelope"></i> 
                <button type="button" class="add-item btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Insertar</button>
            </h4>
        </div>
        <div class="panel-body">
            <div class="container-items"><!-- widgetBody -->
            <?php foreach ($modelsModelo01 as $i => $modelModelo01): ?>
                <div class="item panel panel-default"><!-- widgetItem -->
                    <div class="panel-heading">
                        <h3 class="panel-title pull-left"></h3>
                        <div class="pull-right">
                            <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">
                        <?php
                            // necessary for update action.
                            if (! $modelModelo01->isNewRecord) {
                                echo Html::activeHiddenInput($modelModelo01, "[{$i}]planificacion_fk");
                            }
                        ?>
                        <?= $form->field($modelModelo01, "[{$i}]plan_orden")->textInput(['maxlength' => 255,'style'=>'width:250px;']) ?>
                        <div class="row">
                            <div class="col-sm-6">
                                <?= $form->field($modelModelo01, "[{$i}]modelo01_directiva")->textInput(['maxlength' => 255,'style'=>'width:250px;']) ?>
                            </div>
                            <div class="col-sm-6">
                                <?= $form->field($modelModelo01, "[{$i}]modelo01_fecha_ini_real")->widget(DatePicker::classname(), [
                                    'model' => $modelModelo01,
                                    'attribute' => 'modelo01_fecha_ini_real',
                                    'options'=>['placeholder'=>'Inserte una fecha','style'=>'width:250px;'],
                                    'language' => 'es',
                                    'pluginOptions' => [
                                    'dateFormat' => 'yyyy-MM-dd',
                                     ],
                                    ]); 
                                ?>
                            </div>
                        </div><!-- .row -->
                        <div class="row">
                            <div class="col-sm-4">
                                <?= $form->field($modelModelo01, "[{$i}]modelo01_fecha_fin_real")->widget(DatePicker::classname(), [
                                'model' => $modelModelo01,
                                'attribute' => 'modelo01_fecha_fin_real',
                                'options'=>['placeholder'=>'Inserte una fecha','style'=>'width:250px;'],
                                'language' => 'es',
                                'pluginOptions' => [
                                'dateFormat' => 'yyyy-MM-dd',
                                 ],
                                ]); ?>
                            </div>
                            <div class="col-sm-4">
                                <?= $form->field($modelModelo01, "[{$i}]calificacion_fk")->widget(Select2::classname(), [
                                    'data' => ArrayHelper::map(\backend\models\Ncalificaciones::find()->asArray()->all(), 'id_calificacion', 'calificacion_desc'),
                                    'options' => ['placeholder' => 'Seleccione una calificación ','style'=>'width:250px;'],
                                    'pluginOptions' => [
                                    'allowClear' => true
                                    ],
                                    ]); ?>
                            </div>
                            <div class="col-sm-4">
                                <?= $form->field($modelModelo01, "[{$i}]modelo01_cant_aud_real")->textInput(['style'=>'width:250px;']) ?>
                            </div>
                        </div><!-- .row -->
                        <div class="row">
                            <div class="col-sm-4">
                                <?= $form->field($modelModelo01, "[{$i}]modelo01_dias_real")->textInput(['style'=>'width:250px;']) ?>
                            </div>
                            <div class="col-sm-4">
                                <?= $form->field($modelModelo01, "[{$i}]modelo01_phc")->textInput(['style'=>'width:250px;']) ?>
                            </div>
                            <div class="col-sm-4">
                                <?= $form->field($modelModelo01, "[{$i}]modelo01_phd")->textInput(['style'=>'width:250px;']) ?>
                            </div>
                        </div><!-- .row -->
                        <div class="row">
                            <div class="col-sm-4">
                                <?= $form->field($modelModelo01, "[{$i}]dic_fk")->textInput(['maxlength' => 255,'style'=>'width:250px;']) ?>
                            </div>
                        </div><!-- .row -->
                    </div>
                </div>
            <?php endforeach; ?>
            </div>
        </div>
    </div><!-- .panel -->
    <?php DynamicFormWidget::end(); ?>
 </div>
</div>
</div>
  
  <div class="row">
        <div class="panel panel-default">
        <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Modelo 03</h4></div>
        <div class="panel-body">
             <?php DynamicFormWidget::begin([
                'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
                'widgetBody' => '.container-items2', // required: css class selector
                'widgetItem' => '.item2', // required: css class
                'limit' => 999, // the maximum times, an element can be cloned (default 999)
                'min' => 0, // 0 or 1 (default 1)
                'insertButton' => '.add-item2', // css class
                'deleteButton' => '.remove-item2', // css class
                'model' => $modelsModelo03[0],
                'formId' => 'dynamic-form',
                'formFields' => [
                    'cargo_fk',
                    'medida_apl_fk',
                    'modelo03_cant',
                    'dic_fk',                   
                ],
            ]); ?>

            <div class="panel panel-default">
        <div class="panel-heading">
            <h4>
                <i class="glyphicon glyphicon-envelope"></i> 
                <button type="button" class="add-item2 btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Insertar</button>
            </h4>
        </div>
        <div class="panel-body">
            <div class="container-items2"><!-- widgetBody -->
            <?php foreach ($modelsModelo03 as $i => $modelModelo03): ?>
                <div class="item2 panel panel-default"><!-- widgetItem -->
                    <div class="panel-heading">
                        <h3 class="panel-title pull-left"></h3>
                        <div class="pull-right">
                            <button type="button" class="remove-item2 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 (! $modelModelo03->isNewRecord) {
                                echo Html::activeHiddenInput($modelModelo03, "[{$i}]planificacion_fk");
                            }
                        ?>                        
                        <div class="row">
                            <div class="col-sm-6">
                                <?= $form->field($modelModelo03, "[{$i}]cargo_fk")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-6">
                                <?= $form->field($modelModelo03, "[{$i}]medida_apl_fk")->textInput(['maxlength' => true]) ?>
                            </div>
                        </div><!-- .row -->
                        <div class="row">
                            <div class="col-sm-4">
                                <?= $form->field($modelModelo03, "[{$i}]modelo03_cant")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-4">
                                <?= $form->field($modelModelo03, "[{$i}]dic_fk")->textInput(['maxlength' => true]) ?>
                            </div>
                        </div><!-- .row -->
                    </div>
                </div>
            <?php endforeach; ?>
            </div>
        </div>
    </div><!-- .panel -->
    <?php DynamicFormWidget::end(); ?>
 </div>
</div>
</div>
    
<div class="row">
    <div class="panel panel-default">
    <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Modelo 04</h4></div>
    <div class="panel-body">
         <?php DynamicFormWidget::begin([
            'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
            'widgetBody' => '.container-items3', // required: css class selector
            'widgetItem' => '.item3', // required: css class
            'limit' => 999, // the maximum times, an element can be cloned (default 999)
            'min' => 0, // 0 or 1 (default 1)
            'insertButton' => '.add-item3', // css class
            'deleteButton' => '.remove-item3', // css class
            'model' => $modelsModelo04[0],
            'formId' => 'dynamic-form',
            'formFields' => [
                'categoria_fk',
                'viol_obj_soc_fk',
                'dic_fk',                   
            ],
        ]); ?>

        <div class="panel panel-default">
    <div class="panel-heading">
        <h4>
            <i class="glyphicon glyphicon-envelope"></i> 
            <button type="button" class="add-item3 btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Insertar</button>
        </h4>
    </div>
    <div class="panel-body">
        <div class="container-items3"><!-- widgetBody -->
        <?php foreach ($modelsModelo04 as $i => $modelModelo04): ?>
            <div class="item3 panel panel-default"><!-- widgetItem -->
                <div class="panel-heading">
                    <h3 class="panel-title pull-left"></h3>
                    <div class="pull-right">
                        <button type="button" class="remove-item3 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 (! $modelModelo04->isNewRecord) {
                            echo Html::activeHiddenInput($modelModelo04, "[{$i}]planificacion_fk");
                        }
                    ?>                        
                    <div class="row">
                        <div class="col-sm-6">
                            <?= $form->field($modelModelo04, "[{$i}]categoria_fk")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-6">
                            <?= $form->field($modelModelo04, "[{$i}]viol_obj_soc_fk")->textInput(['maxlength' => true]) ?>
                        </div>
                    </div><!-- .row -->
                    <div class="row">
                        <div class="col-sm-4">
                            <?= $form->field($modelModelo04, "[{$i}]dic_fk")->textInput(['maxlength' => true]) ?>
                        </div>
                    </div><!-- .row -->
                </div>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
</div><!-- .panel -->
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>    
        
    <div class="row">
    <div class="panel panel-default">
    <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Modelo 05</h4></div>
    <div class="row panel-body">
        <?php DynamicFormWidget::begin([
            'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
            'widgetBody' => '.container-items4', // required: css class selector
            'widgetItem' => '.item4', // required: css class
            'limit' => 999, // the maximum times, an element can be cloned (default 999)
            'min' => 0, // 0 or 1 (default 1)
            'insertButton' => '.add-item4', // css class
            'deleteButton' => '.remove-item4', // css class
            'model' => $modelsModelo05[0],
            'formId' => 'dynamic-form',
            'formFields' => [
                'modelo05_no_registro',
                'situacion_fk',
                'modelo05_sintesis',
                'modelo05_causas',
                'dic_fk',   
            ],
        ]); ?>
                
        <table class="table table-bordered">
            <thead>
                <tr class="active">
                    <td></td>
                    <td><?= Html::activeLabel($modelsModelo05[0], 'modelo05_no_registro'); ?></td>
                    <td><?= Html::activeLabel($modelsModelo05[0], 'situacion_fk'); ?></td>
                    <td><?= Html::activeLabel($modelsModelo05[0], 'modelo05_sintesis'); ?></td>
                    <td><?= Html::activeLabel($modelsModelo05[0], 'modelo05_causas'); ?></td>
                    <td><?= Html::activeLabel($modelsModelo05[0], 'dic_fk'); ?></td>
                    <td><label class="control-label">Implicados</label></td>
                </tr>
            </thead>

            <tbody class="container-items4"><!-- widgetContainer -->
            <?php foreach ($modelsModelo05 as $i => $modelModelo05): ?>
                <tr class="item4"><!-- widgetBody -->
                    <td>
                        <button type="button" class="remove-item4 btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
                        <?php
                        // necessary for update action.
                        if (! $modelModelo05->isNewRecord) {
                            echo Html::activeHiddenInput($modelModelo05, "[{$i}]planificacion_fk");
                        }
                        ?>
                    </td>
                    <td>
                        <?php
                            echo $form->field($modelModelo05, "[{$i}]modelo05_no_registro")->begin();
                            echo Html::activeTextInput($modelModelo05, "[{$i}]modelo05_no_registro", ['maxlength' => true, 'class' => 'form-control']); //Field
                            echo Html::error($modelModelo05,"[{$i}]modelo05_no_registro", ['class' => 'help-block']); //error
                            echo $form->field($modelModelo05, "[{$i}]modelo05_no_registro")->end();
                        ?>
                    </td>
                    <td>
                        <?php
                            echo $form->field($modelModelo05, "[{$i}]situacion_fk")->begin();
                            echo Html::activeTextInput($modelModelo05, "[{$i}]situacion_fk", ['maxlength' => true, 'class' => 'form-control']); //Field
                            echo Html::error($modelModelo05,"[{$i}]situacion_fk", ['class' => 'help-block']); //error
                            echo $form->field($modelModelo05, "[{$i}]situacion_fk")->end();
                        ?>
                    </td>
                    <td>
                        <?php
                            echo $form->field($modelModelo05, "[{$i}]modelo05_sintesis")->begin();
                            echo Html::activeTextInput($modelModelo05, "[{$i}]modelo05_sintesis", ['maxlength' => true, 'class' => 'form-control']); //Field
                            echo Html::error($modelModelo05,"[{$i}]modelo05_sintesis", ['class' => 'help-block']); //error
                            echo $form->field($modelModelo05, "[{$i}]modelo05_sintesis")->end();
                        ?>
                    </td>
                    <td>
                        <?php
                            echo $form->field($modelModelo05, "[{$i}]modelo05_causas")->begin();
                            echo Html::activeTextInput($modelModelo05, "[{$i}]modelo05_causas", ['maxlength' => true, 'class' => 'form-control']); //Field
                            echo Html::error($modelModelo05,"[{$i}]modelo05_causas", ['class' => 'help-block']); //error
                            echo $form->field($modelModelo05, "[{$i}]modelo05_causas")->end();
                        ?>
                    </td>
                    <td>
                        <?php
                            echo $form->field($modelModelo05, "[{$i}]dic_fk")->begin();
                            echo Html::activeTextInput($modelModelo05, "[{$i}]dic_fk", ['maxlength' => true, 'class' => 'form-control']); //Field
                            echo Html::error($modelModelo05,"[{$i}]dic_fk", ['class' => 'help-block']); //error
                            echo $form->field($modelModelo05, "[{$i}]dic_fk")->end();
                        ?>
                    </td>

<!-- Implicados -->
                    <td id="implicados">

                        <?php DynamicFormWidget::begin([
                            'widgetContainer' => 'dynamicform_inner', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
                            'widgetBody' => '.container-items5', // required: css class selector
                            'widgetItem' => '.item5', // required: css class
                            'limit' => 999, // the maximum times, an element can be cloned (default 999)
                            'min' => 1, // 0 or 1 (default 1)
                            'insertButton' => '.add-item5', // css class
                            'deleteButton' => '.remove-item5', // css class
                            'model' => $modelsImplicados[$i][0],
                            'formId' => 'dynamic-form',
                            'formFields' => [
                                'implicados_nombre',
                                'implicados_carnet',
                                'cargo_fk',
                                'cat_ocupacional_fk',
                                'nivel_dir_fk',
                                'escolaridad_fk',
                                'implicados_pertenece',
                                'dic_fk',
                            ],
                        ]);

                        ?>
                       
                        <table class="table table-bordered">
                            <thead>
                                <tr class="active">
                                    <td></td>
                                    <td><?= Html::activeLabel($modelsImplicados[$i][0], 'implicados_nombre'); ?></td>
                                    <td><?= Html::activeLabel($modelsImplicados[$i][0], 'implicados_carnet'); ?></td>
                                    <td><?= Html::activeLabel($modelsImplicados[$i][0], 'cargo_fk'); ?></td>
                                    <td><?= Html::activeLabel($modelsImplicados[$i][0], 'cat_ocupacional_fk'); ?></td>
                                    <td><?= Html::activeLabel($modelsImplicados[$i][0], 'nivel_dir_fk'); ?></td>
                                    <td><?= Html::activeLabel($modelsImplicados[$i][0], 'escolaridad_fk'); ?></td>
                                    <td><?= Html::activeLabel($modelsImplicados[$i][0], 'implicados_pertenece'); ?></td>
                                    <td><?= Html::activeLabel($modelsImplicados[$i][0], 'dic_fk'); ?></td>
                                </tr>
                            </thead>
                            <tbody class="container-items5"><!-- widgetContainer -->
                            <?php foreach ($modelsImplicados[$i] as $ix => $modelImplicados): ?>
                                <tr class="item5"><!-- widgetBody -->
                                    <td>
                                        <button type="button" class="remove-item5 btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
                                        <?php
                                        // necessary for update action.
                                        if (! $modelImplicados->isNewRecord) {
                                            echo Html::activeHiddenInput($modelImplicados, "[{$i}][{$ix}]modelo05_fk");
                                        }
                                        ?>
                                    </td>

                                    <td>
                                        <?php
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]implicados_nombre")->begin();
                                            echo Html::activeTextInput($modelImplicados, "[{$i}][{$ix}]implicados_nombre", ['maxlength' => true, 'class' => 'form-control']); //Field
                                            echo Html::error($modelImplicados,"[{$i}][{$ix}]implicados_nombre", ['class' => 'help-block']); //error
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]implicados_nombre")->end();
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]implicados_carnet")->begin();
                                            echo Html::activeTextInput($modelImplicados, "[{$i}][{$ix}]implicados_carnet", ['maxlength' => true, 'class' => 'form-control']); //Field
                                            echo Html::error($modelImplicados,"[{$i}][{$ix}]implicados_carnet", ['class' => 'help-block']); //error
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]implicados_carnet")->end();
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]cargo_fk")->begin();
                                            echo Html::activeTextInput($modelImplicados, "[{$i}][{$ix}]cargo_fk", ['maxlength' => true, 'class' => 'form-control']); //Field
                                            echo Html::error($modelImplicados,"[{$i}][{$ix}]cargo_fk", ['class' => 'help-block']); //error
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]cargo_fk")->end();
                                        ?>
                                    </td>
                                    <td>
                                        <?php                                            
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]cat_ocupacional_fk")->begin();
                                            echo Html::activeTextInput($modelImplicados, "[{$i}][{$ix}]cat_ocupacional_fk", ['maxlength' => true, 'class' => 'form-control']); //Field
                                            echo Html::error($modelImplicados,"[{$i}][{$ix}]cat_ocupacional_fk", ['class' => 'help-block']); //error
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]cat_ocupacional_fk")->end();
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]nivel_dir_fk")->begin();
                                            echo Html::activeTextInput($modelImplicados, "[{$i}][{$ix}]nivel_dir_fk", ['maxlength' => true, 'class' => 'form-control']); //Field
                                            echo Html::error($modelImplicados,"[{$i}][{$ix}]nivel_dir_fk", ['class' => 'help-block']); //error
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]nivel_dir_fk")->end();
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]escolaridad_fk")->begin();
                                            echo Html::activeTextInput($modelImplicados, "[{$i}][{$ix}]escolaridad_fk", ['maxlength' => true, 'class' => 'form-control']); //Field
                                            echo Html::error($modelImplicados,"[{$i}][{$ix}]escolaridad_fk", ['class' => 'help-block']); //error
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]escolaridad_fk")->end();
                                        ?>
                                    </td>
                                    <td>
                                        <?php

                                           /*if(!$modelImplicados->isNewRecord) {
                                                $modelImplicados->implicados_pertenece = $modelImplicados->readLoadImplicados_pertenece();
                                            }*/
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]implicados_pertenece")->begin();
                                            echo Html::activeTextInput($modelImplicados, "[{$i}][{$ix}]implicados_pertenece", ['maxlength' => true, 'class' => 'form-control']); //Field
                                            echo Html::error($modelImplicados,"[{$i}][{$ix}]implicados_pertenece", ['class' => 'help-block']); //error
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]implicados_pertenece")->end();
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]dic_fk")->begin();
                                            echo Html::activeTextInput($modelImplicados, "[{$i}][{$ix}]dic_fk", ['maxlength' => true, 'class' => 'form-control']); //Field
                                            echo Html::error($modelImplicados,"[{$i}][{$ix}]dic_fk", ['class' => 'help-block']); //error
                                            echo $form->field($modelImplicados, "[{$i}][{$ix}]dic_fk")->end();
                                        ?>
                                    </td>

                                </tr>
                            <?php endforeach; // end of implicados loop ?>
                            </tbody>
                            <tfoot>
                                <td colspan="9" class="active"><button type="button" class="add-item5 btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button></td>
                            </tfoot>
                        </table>
                        <?php DynamicFormWidget::end(); // end of loads widget ?>

                    </td> <!-- implicados sub column -->
                </tr><!-- payment -->
            <?php endforeach; // end of modelo05 loop ?>
            </tbody>
            <tfoot>
                <td colspan="7" class="active">
                    <button type="button" class="add-item4 btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
                </td>
            </tfoot>
            </table>
        <?php DynamicFormWidget::end(); // end of modelo05 widget ?>
  </div>
 </div>
</div>
    
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

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

</div>
<script type="text/javascript">
    $(".dynamicform_wrapper").on("beforeInsert", function(e, item) {
    console.log("beforeInsert");
});
 
$(".dynamicform_wrapper").on("afterInsert", function(e, item) {
    console.log("afterInsert");
});
 
$(".dynamicform_wrapper").on("beforeDelete", function(e, item) {
    if (! confirm("Are you sure you want to delete this item?")) {
        return false;
    }
    return true;
});
 
$(".dynamicform_wrapper").on("afterDelete", function(e) {
    console.log("Deleted item!");
});
 
$(".dynamicform_wrapper").on("limitReached", function(e, item) {
    alert("Limit reached");
});
</script>



controller
<?php

namespace backend\controllers;

use Yii;
use backend\models\Planificacion;
use backend\models\PlanificacionSearch;
use backend\models\Modelo01;
use backend\models\Modelo03;
use backend\models\Modelo04;
use backend\models\Modelo05;
use backend\models\Implicados;
use backend\models\Model;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use mPDF;

/**
 * PlanificacionController implements the CRUD actions for Planificacion model.
 */
class PlanificacionController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Planificacion models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new PlanificacionSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Planificacion model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Planificacion model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Planificacion();
        $modelsModelo01 = [new Modelo01];
        $modelsModelo03 = [new Modelo03];
        $modelsModelo04 = [new Modelo04];
        $modelsModelo05 = [new Modelo05()];
        $modelsImplicados[] = [new Implicados];

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

            $modelsModelo01 = Model::createMultiple(Modelo01::classname());
            \yii\base\Model::loadMultiple($modelsModelo01, Yii::$app->request->post());
            $modelsModelo03 = Model::createMultiple(Modelo03::classname());
            \yii\base\Model::loadMultiple($modelsModelo03, Yii::$app->request->post());
            $modelsModelo04 = Model::createMultiple(Modelo04::classname());
            \yii\base\Model::loadMultiple($modelsModelo04, Yii::$app->request->post());
            $modelsModelo05 = Model::createMultiple(Modelo05::classname());
            \yii\base\Model::loadMultiple($modelsModelo05, Yii::$app->request->post());

                  
            // get Implicados data from POST
            $implicadosData['_csrf'] =  Yii::$app->request->post()['_csrf'];
            for ($i=0; $i<count($modelsModelo05); $i++) {
                $implicadosData['Implicados'] =  Yii::$app->request->post()['Implicados'][$i];
                $modelsImplicados[$i] = Model::createMultiple(Implicados::classname(),[] ,$implicadosData);
                Model::loadMultiple($modelsImplicados[$i], $implicadosData);
            }

            // validate all models
            $valid = $model->validate();
            $valid = \yii\base\Model::validateMultiple($modelsModelo01) && $valid;
            $valid1 = $model->validate();
            $valid1 = \yii\base\Model::validateMultiple($modelsModelo03) && $valid1;
            $valid2 = $model->validate();
            $valid2 = \yii\base\Model::validateMultiple($modelsModelo04) && $valid2;
            $valid3 = $model->validate();
            $valid3 = Modelo05::validateModelo($modelsModelo05,$modelsImplicados) && $valid3;

            if ($valid1 && $valid1 && $valid2 ) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        foreach ($modelsModelo01 as $modelModelo01)
                            {
                            $modelModelo01->planificacion_fk = $model->plan_cod;
                            if (! ($flag = $modelModelo01->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                        foreach ($modelsModelo03 as $modelModelo03) 
                            {
                            $modelModelo03->planificacion_fk = $model->plan_cod;
                            if (! ($flag = $modelModelo03->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                        foreach ($modelsModelo04 as $modelModelo04)
                            {
                            $modelModelo04->planificacion_fk = $model->plan_cod;
                            if (! ($flag = $modelModelo04->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                        foreach ($modelsModelo05 as $i => $modelModelo05) {
                            // save the Modelo05 record
                            $modelModelo05->planificacion_fk = $model->plan_cod;
                            if ($flag = $modelModelo05->save(false)) {
                                // loop through each Implicados
                                foreach ($modelsImplicados[$i] as $ix => $modelImplicados) {
                                    // save the load record
                                    $modelImplicados->modelo05_fk = $modelModelo05->modelo05_no_registro;
                                    if (! ($flag = $modelImplicados->save(false))) {
                                        $transaction->rollBack();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if ($flag) {
                        $transaction->commit();
                        return $this->redirect(['view', 'id' => $model->plan_cod]);
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
        } else {
            return $this->render('create', [
                'model' => $model,
                'modelsModelo01' => (empty($modelsModelo01)) ? [new Modelo01] : $modelsModelo01,
                'modelsModelo03' => (empty($modelsModelo03)) ? [new Modelo03] : $modelsModelo03,
                'modelsModelo04' => (empty($modelsModelo04)) ? [new Modelo04] : $modelsModelo04,
                'modelsModelo05' => (empty($modelsModelo05)) ? [new Modelo05] : $modelsModelo05,
                'modelsImplicados' => (empty($modelsImplicados)) ? [new Implicados] : $modelsImplicados,
            ]);
        }
    }

    /**
     * Updates an existing Planificacion model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $modelsModelo01 = $model->modelo01;
        $modelsModelo03 = $model->modelo03s;
        $modelsModelo04 = $model->modelo04s;
        

        if ($model->load(Yii::$app->request->post()) /*&& $model->save()*/) 
        {
            //modelo01
            $oldIDs = ArrayHelper::map($modelsModelo01, 'planificacion_fk', 'planificacion_fk');
            $modelsModelo01 = Model::createMultiple(Modelo01::classname(), $modelsModelo01);
            Model::loadMultiple($modelsModelo01, Yii::$app->request->post());
            $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsModelo01, 'planificacion_fk', 'planificacion_fk')));
            
            //modelo03
            $oldIDs1 = ArrayHelper::map($modelsModelo03, 'planificacion_fk', 'planificacion_fk');
            $modelsModelo03 = Model::createMultiple(Modelo03::classname(), $modelsModelo03);
            Model::loadMultiple($modelsModelo03, Yii::$app->request->post());
            $deletedIDs1 = array_diff($oldIDs1, array_filter(ArrayHelper::map($modelsModelo03, 'planificacion_fk', 'planificacion_fk')));
            
            //modelo04
            $oldIDs2 = ArrayHelper::map($modelsModelo04, 'planificacion_fk', 'planificacion_fk');
            $modelsModelo04 = Model::createMultiple(Modelo04::classname(), $modelsModelo04);
            Model::loadMultiple($modelsModelo04, Yii::$app->request->post());
            $deletedIDs2 = array_diff($oldIDs2, array_filter(ArrayHelper::map($modelsModelo04, 'planificacion_fk', 'planificacion_fk')));

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

            // validate all models
            $valid = $model->validate();
            $valid = Model::validateMultiple($modelsModelo01) && $valid;
            $valid1 = $model->validate();
            $valid1 = Model::validateMultiple($modelsModelo03) && $valid1;
            $valid2 = $model->validate();
            $valid2 = Model::validateMultiple($modelsModelo04) && $valid2;

            if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        if (! empty($deletedIDs) && ! empty($deletedIDs1) && ! empty($deletedIDs2)) {
                            Modelo01::deleteAll(['planificacion_fk' => $deletedIDs]);
                            Modelo03::deleteAll(['planificacion_fk' => $deletedIDs1]);
                            Modelo04::deleteAll(['planificacion_fk' => $deletedIDs2]);
                        }
                        foreach ($modelsModelo01 as $modelModelo01) {
                            $modelModelo01->planificacion_fk = $model->plan_cod;
                            if (! ($flag = $modelModelo01->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                        foreach ($modelsModelo03 as $modelModelo03) {
                            $modelModelo03->planificacion_fk = $model->plan_cod;
                            if (! ($flag = $modelModelo03->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                        foreach ($modelsModelo04 as $modelModelo04) {
                            $modelModelo04->planificacion_fk = $model->plan_cod;
                            if (! ($flag = $modelModelo04->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                    }
                    if ($flag) {
                        $transaction->commit();
                        return $this->redirect(['view', 'id' => $model->plan_cod]);
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
        } else {
            return $this->render('update', [
                'model' => $model,
                'modelsModelo01' => (empty($modelsModelo01)) ? [new Modelo01] : $modelsModelo01,
                'modelsModelo03' => (empty($modelsModelo03)) ? [new Modelo03] : $modelsModelo03,
                'modelsModelo04' => (empty($modelsModelo04)) ? [new Modelo04] : $modelsModelo04
            ]);
        }
    }

    /**
     * Deletes an existing Planificacion model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Planificacion model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Planificacion the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Planificacion::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionLists($id)
    {
        $rows = \app\models\Entidad::find()->where(['orga_fk' => $id])->all();
 
        echo "<option>Selecione una entidad</option>";
 
        if(count($rows)>0){
            foreach($rows as $row){
                echo "<option value='$row->id_entidad'>$row->entidad_nombre</option>";
            }
        }
        else{
            echo "<option>No existe ninguna entidad</option>";
        } 
    }
}


View Postfsnxdi, on 06 November 2015 - 09:33 AM, said:

i didn`t used it, but:
remember that when you add multiple records to your with for example foreach you have to do:
$modelOfTableUWantToAddReccord->isNewRecord = true;


0

#7 User is offline   vishwasrao 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 07-September 14

Posted 10 November 2015 - 07:24 AM

Hi,
I have used this extension in my project & its working as expected.
I have replied in forum https://github.com/w...cform/issues/66 with update section.
There are video tutorials for same
https://www.youtube....h?v=qqFpTBr323Y

Thanks,
Vishwas
0

#8 User is offline   epulgaron 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 168
  • Joined: 19-February 14

Posted 11 November 2015 - 08:59 AM

hi, thx for your answer but my problem isn't with the actionUpdate i can update without any problem , my issue is when i try to insert more than one element(in my case Modelo01)only store in DB the last one, not all of them, also when i use karktik-Select2 doesn't work, it stays loading and don't show the widget but if i use a dropdown it work fine, but i need to use Select2, and when i try to use kartik-DatePicker it shows but the calendar doesn't work,i'm going to attach an image, plz help, i really see this extension very useful and i want to use it,thx and sorry for my bad english

View Postvishwasrao, on 10 November 2015 - 07:24 AM, said:

Hi,
I have used this extension in my project & its working as expected.
I have replied in forum https://github.com/w...cform/issues/66 with update section.
There are video tutorials for same
https://www.youtube....h?v=qqFpTBr323Y

Thanks,
Vishwas

Attached File(s)


0

#9 User is offline   lukBB 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 08-December 10

Posted 04 January 2017 - 11:09 AM

Hi epulgaron,

Have you managed to solve the issue with having more than one DynamicForm in one view ?

I have similar issue in the create action. I have two DynamicForms in one form. In create action only items from first DynamicForm are beeing saved. Second DynamicForm is creating empty items with ID only if though I removed all the validation rules.

Here is my view code:
 <div class="panel panel-default">
        <div class="panel-heading">
            <h4>
                <i class="glyphicon glyphicon-envelope"></i> Addresses
                <button type="button" class="add-item btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Add</button>
            </h4>
        </div>
        <div class="panel-body">
            <div class="container-items"><!-- widgetBody -->
            <?php foreach ($modelsAddress as $i => $modelAddress): ?>
                <div class="item panel panel-default"><!-- widgetItem -->
                    <div class="panel-heading">
                        <h3 class="panel-title pull-left">Address</h3>
                        <div class="pull-right">
                            <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">
                        <?php
                            // necessary for update action.
                            if (! $modelAddress->isNewRecord) {
                                echo Html::activeHiddenInput($modelAddress, "[{$i}]id");
                            }
                        ?>
                        <?= $form->field($modelAddress, "[{$i}]title")->textInput(['maxlength' => true]) ?>
                        <div class="row">
                            <div class="col-sm-6">
                                <?= $form->field($modelAddress, "[{$i}]country")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-6">
                                <?= $form->field($modelAddress, "[{$i}]province")->textInput(['maxlength' => true]) ?>
                            </div>
                        </div><!-- .row -->
                        <div class="row">
                            <div class="col-sm-3">
                                <?= $form->field($modelAddress, "[{$i}]city")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-3">
                                <?= $form->field($modelAddress, "[{$i}]post_code")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-3">
                                <?= $form->field($modelAddress, "[{$i}]street")->textInput(['maxlength' => true]) ?>
                            </div>
							<div class="col-sm-3">
                                <?= $form->field($modelAddress, "[{$i}]street_number")->textInput(['maxlength' => true]) ?>	
                            </div>
                        </div><!-- .row -->
                    </div>
                </div>
            <?php endforeach; ?>
            </div>
        </div>
    </div><!-- .panel -->
    <?php DynamicFormWidget::end(); ?>


<h2>Triathlon</h2>

    <?php DynamicFormWidget::begin([
        'widgetContainer' => 'dynamicform_wrapper_tri', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
        'widgetBody' => '.container-items', // required: css class selector
        'widgetItem' => '.item', // required: css class
        'limit' => 5, // the maximum times, an element can be added (default 999)
        'min' => 1, // 0 or 1 (default 1)
        'insertButton' => '.add-item', // css class
        'deleteButton' => '.remove-item', // css class
        'model' => $modelsTriathlonType[0],
        'formId' => 'dynamic-form',
        'formFields' => [
            'title',
            'swim',
            'bike',
            'run',
        ],
    ]); ?>

    <div class="panel panel-default">
        <div class="panel-heading">
            <h4>
                <i class="glyphicon glyphicon-envelope"></i> Triathlons
                <button type="button" class="add-item btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Add</button>
            </h4>
        </div>
        <div class="panel-body">
            <div class="container-items"><!-- widgetBody -->
            <?php foreach ($modelsTriathlonType as $iTriathlon => $modelTriathlonType): ?>
                <div class="item panel panel-default"><!-- widgetItem -->
                    <div class="panel-heading">
                        <h3 class="panel-title pull-left">Triathlon</h3>
                        <div class="pull-right">
                            <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">
                        <?php
                            // necessary for update action.
                            if (! $modelTriathlonType->isNewRecord) {
                                echo Html::activeHiddenInput($modelTriathlonType, "[{$iTriathlon}]id");
                            }
                        ?>
                        <?= $form->field($modelTriathlonType, "[{$iTriathlon}]title")->textInput(['maxlength' => true]) ?>
                        <div class="row">
                            <div class="col-sm-4">
                                <?= $form->field($modelTriathlonType, "[{$iTriathlon}]swim")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-4">
                                <?= $form->field($modelTriathlonType, "[{$iTriathlon}]bike")->textInput(['maxlength' => true]) ?>
                            </div>
							 <div class="col-sm-4">
                                <?= $form->field($modelTriathlonType, "[{$iTriathlon}]run")->textInput(['maxlength' => true]) ?>
                            </div>
                        </div><!-- .row -->
                    </div>
                </div>
            <?php endforeach; ?>
            </div>
        </div>
    </div><!-- .panel -->
    <?php DynamicFormWidget::end(); ?>


Here is my controller create action code:
public function actionCreate()
    {
        $model = new Contest();
		if (!$model) {
            throw new NotFoundHttpException("The contest was not found.");
        }
		$modelsAddress = [new Address];
		if (!$modelsAddress) {
            throw new NotFoundHttpException("The address was not found.");
        }
		$date = new Date();
		if (!$date) {
            throw new NotFoundHttpException("The date was not found.");
        }
		$seo = new Seo();
		if (!$seo) {
            throw new NotFoundHttpException("The seo was not found.");
        }
		$modelsTriathlonType = [new TriathlonType];
		if (!$modelsTriathlonType) {
            throw new NotFoundHttpException("The triathlon was not found.");
        }
		
		if ($model->load(Yii::$app->request->post()) && $seo->load(Yii::$app->request->post()) && $date->load(Yii::$app->request->post())) {
            
			$modelsAddress = Model::createMultiple(Address::classname());
			$modelsTriathlonType = Model::createMultiple(TriathlonType::classname());
            Model::loadMultiple($modelsAddress, Yii::$app->request->post());
            Model::loadMultiple($modelsTriathlonType, Yii::$app->request->post());
			
			 // ajax validation
            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = Response::FORMAT_JSON;
                return ArrayHelper::merge(
                    ActiveForm::validateMultiple($modelsAddress, $modelsTriathlonType, $model, $seo, $date)
                );
            }	
			
			$isValid = $model->validate();
			if(!$isValid) {
				throw new NotFoundHttpException("Model validation issue.");
			}
			$isValid = Model::validateMultiple($modelsTriathlonType, ['title']) && $isValid;
			if(!$isValid) {
				throw new NotFoundHttpException("TT validation issue.");
			}
            $isValid = Model::validateMultiple($modelsAddress) && $isValid;
			if(!$isValid) {
				throw new NotFoundHttpException("Adres validation issue.");
			}
            $isValid = $seo->validate() && $isValid;	
			if($isValid = false) {
				throw new NotFoundHttpException("Seo validation issue.");
			}
            if ($isValid) {
				$transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save()) {
                        foreach ($modelsAddress as $address) {
                            if($address->save()) {
								$contestHasAddress = new ContestHasAddress;
								$contestHasAddress->contest_id = $model->id;
								$contestHasAddress->address_id = $address->id;
								$contestHasAddress->save(false);
							}
                            if (! ($flag = $address->save())) {
                                $transaction->rollBack();
                                break;
                            }
                        }
						foreach ($modelsTriathlonType as $triathlon) {
                            if($triathlon->save()) {
								$contestHasTriathlonType = new ContestHasTriathlonType;
								$contestHasTriathlonType->contest_id = $model->id;
								$contestHasTriathlonType->triathlon_type_id = $triathlon->id;
								$contestHasTriathlonType->save(false);
							}
                            if (! ($flag = $triathlon->save())) {
                                $transaction->rollBack();
                                break;
                            }
                        }
						
						if(isset($date->date)) {
							$date->date = strtotime($date->date);
						}
						if(isset($date->start_time)) {
							$date->start_time = strtotime($date->start_time);
						}
						if(isset($date->end_time)) {
							$date->end_time = strtotime($date->end_time);
						}
						
						if($date->save()) {
							$contestHasDate = new ContestHasDate;
							$contestHasDate->contest_id = $model->id;
							$contestHasDate->date_id = $date->id;
							$contestHasDate->save(false);
						}
						
						if($seo->save()) {
							$contestHasSeo = new ContestHasSeo;
							$contestHasSeo->contest_id = $model->id;
							$contestHasSeo->seo_id = $seo->id;
							$contestHasSeo->save(false);
						}
							
                    }
                    if ($flag) {
                        $transaction->commit();
                        return $this->redirect(['contest/view', 'id' => $model->id]);
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
			
        }
		else {
            return $this->render('create', [
                'model' => $model,
                'modelsAddress' => (empty($modelsAddress)) ? [new Address] : $modelsAddress,
                'date' => $date,
                'seo' => $seo,
                'modelsTriathlonType' => (empty($modelsTriathlonType)) ? [new TriathlonType] : $modelsTriathlonType,
            ]);
        }
    }


Here is the potential issue I have no idea to solve. When I click to add new item on first or on the second model it is messing the indexes in the view file. I end up with wrong indexes(at least I think this is an issue. Please have a look in the log entry. Second model id in the $_POST data is incrementally increased. So In the TriathlonType the id in $_POST supposed to be 0 and 1 and it is 2 and 3. I'm sorry if this is not clear
$_POST = [
    '_csrf-backend' => 'eDFhaDhWSEowVDMYdT8BIRpAUARrbmUOG0FVLw8.ZQ5KSAZfaCEFfQ=='
    'Contest' => [
        'title' => 'test'
        'lead' => 'test'
        'description' => 'test'
        'payment' => 'test'
        'link' => 'test'
        'logo' => 'test'
        'sponor' => 'test'
        'promo' => '1'
        'featured' => '1'
    ]
    'Address' => [
        0 => [
            'title' => 'test'
            'country' => 'test'
            'province' => ''
            'city' => 'test'
            'post_code' => ''
            'street' => 'test'
            'street_number' => ''
        ]
    ]
    'Date' => [
        'date' => '04-1-2017 15:08'
        'start_time' => '04-1-2017 15:08'
        'end_time' => '04-1-2017 15:08'
        'description' => 'test'
    ]
    'Seo' => [
        'title' => 'test'
        'description' => 'test'
        'keywords' => 'test'
        'author' => ''
    ]
    'TriathlonType' => [
        [color="#FFFF00"]1[/color] => [
            'title' => '1/2 Iron Man'
            'swim' => '1900m'
            'bike' => '90km'
            'run' => '21km'
        ]
        [color="#FFFF00"]2[/color] => [
            'title' => '1/4 Iron Man'
            'swim' => '950m'
            'bike' => '45km'
            'run' => '10.5km'
        ]
    ]
]


If any of you have an advice, please le me know. Cheers

best regards
lukBB



View Postepulgaron, on 11 November 2015 - 08:59 AM, said:

hi, thx for your answer but my problem isn't with the actionUpdate i can update without any problem , my issue is when i try to insert more than one element(in my case Modelo01)only store in DB the last one, not all of them, also when i use karktik-Select2 doesn't work, it stays loading and don't show the widget but if i use a dropdown it work fine, but i need to use Select2, and when i try to use kartik-DatePicker it shows but the calendar doesn't work,i'm going to attach an image, plz help, i really see this extension very useful and i want to use it,thx and sorry for my bad english

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users