yii2-dynamicform

I cant seem to figure out the issue here, data is successfully being sent to the Customer table but nothing is being sent to the branches table.

The code in my controller:




    public function actionCreate()

    {

        $model = new Customer();

        $modelsAddress = [new Address];


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

            

          {

            //dynamic form code to post information to the database

            $modelsAddress = Model::createMultiple(Address::classname());

            Model::loadMultiple($modelsAddress, Yii::$app->request->post());


            // validate all models

            $valid = $model->validate();

            $valid = Model::validateMultiple($modelsAddress) && $valid;


            if ($valid) {

                $transaction = \Yii::$app->db->beginTransaction();

                try {

                    if ($flag = $model->save(false)) {

                        foreach ($modelsAddress as $modelAddress) {

                            $modelAddress->address_id = $model->id;

                            if (! ($flag = $modelAddress->save(false))) {

                                $transaction->rollBack();

                                break;

                            }

                        }

                    }

                    if ($flag) {

                        $transaction->commit();

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

                    }

                } catch (Exception $e) {

                    $transaction->rollBack();

                }

            }

            

        } 

        else {

            return $this->render('create', [

                'model' => $model,

                'modelsAddress' => (empty($modelsAddress)) ? [new Address] : $modelsAddress

            ]);

        }

    }



The Code in the view:




<div class="row">

        <div class="panel panel-default">

        <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Branches</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' => 4, // the maximum times, an element can be cloned (default 999)

                'min' => 1, // 0 or 1 (default 1)

                'insertButton' => '.add-item', // css class

                'deleteButton' => '.remove-item', // css class

                'model' => $modelsAddress[0],

                'formId' => 'dynamic-form',

                'formFields' => [

                    'address_name',

                    'address_street',

                    'address_city',

                    'address_state',

                    'address_postalcode',

                    

                ],

            ]); ?>


            <div class="container-items"><!-- widgetContainer -->

            <?php foreach ($modelsAddress as $i => $modelAddress): ?>

                <div class="item panel panel-default"><!-- widgetBody -->

                    <div class="panel-heading">

                        <h3 class="panel-title pull-left">Branch</h3>

                        <div class="pull-right">

                            <button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>

                            <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}]full_name")->textInput(['maxlength' => true])--> 

                        <div class="row">

                            <div class="col-sm-6">

                                <?= $form->field($modelAddress, "[{$i}]address_name")->textInput(['maxlength' => true]) ?>

                            </div>

                            <div class="col-sm-6">

                                <?= $form->field($modelAddress, "[{$i}]address_street")->textInput(['maxlength' => true]) ?>

                            </div>

                        </div><!-- .row -->

                        <div class="row">

                            <div class="col-sm-4">

                                <?= $form->field($modelAddress, "[{$i}]address_city")->textInput(['maxlength' => true]) ?>

                            </div>

                            <div class="col-sm-4">

                                <?= $form->field($modelAddress, "[{$i}]address_state")->textInput(['maxlength' => true]) ?>

                            </div>

                            <div class="col-sm-4">

                                <?= $form->field($modelAddress, "[{$i}]address_postalcode")->textInput(['maxlength' => true]) ?>

                            </div>

                        </div><!-- .row -->

                    </div>

                </div>

            <?php endforeach; ?>

            </div>

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



I have setup the Model re the usage on wbraganca’s github usage and also removed customer_id from the required section in Address Model. Cant seem to figure out the issue. :( :(

Hello, check id in your ActiveForm is the same at your DynamicForm




$form = ActiveForm::begin([

    'id' => 'dynamic-form',

]);



Hi in your controller:

change

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

to

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