yii 2 wbraganca dynamic form actionCreate does not work

I tried to use wbraganca/yii2-dynamicform on my project everything is well done, but when I click on create button to validate actionCreate nothing happened.

project structure:

suppliers (sup_id, sup_name …)

purchases(purchase_id,sup_id,purchase_date) sup_id is a foreign key

purchases_items(purchase_item_id,purchase_id,item_id,qte) purchase_id and item_id are foreign key

Items(item_id,…)

what I want to do is when user create a purchases, he will be able to create purchases_items at same, and after that update column stock from table items by incrementing with purchases_items’s qte value of corresponding item_id .

NB: column stock is the stock level of items. The project is about stock management.

[html] _form.php

<?php

use yii\helpers\Html;

use yii\widgets\ActiveForm;

use dosamigos\datepicker\DatePicker;

use wbraganca\dynamicform\DynamicFormWidget;

//use yii\jui\DatePicker;

use yii\helpers\ArrayHelper;

use backend\models\suppliers;

use backend\models\Items;

/* @var $this yii\web\View */

/* @var $model backend\models\Purchases */

/* @var $form yii\widgets\ActiveForm */

?>

<div class="purchases-form">

&lt;?php &#036;form = ActiveForm::begin(['id' =&gt; 'dynamic-form']); ?&gt;

<?= $form->field($modelPurchases, ‘purchase_date’)->widget(

    DatePicker::className(), [


        // inline too, not bad


         'inline' =&gt; false, 


         // modify template for custom rendering


        //'template' =&gt; '&lt;div class=&quot;well well-sm&quot; style=&quot;background-color: #fff; width:250px&quot;&gt;{input}&lt;/div&gt;',


        'clientOptions' =&gt; [


            'autoclose' =&gt; true,


            'format' =&gt; 'dd-M-yyyy'


        ]


]);?&gt;





 &lt;?= &#036;form-&gt;field(&#036;modelPurchases, 'sup_id')-&gt;dropDownList(


    ArrayHelper::map(suppliers::find()-&gt;all(),'sup_id','sup_name'),['prompt'=&gt;'Select Fournisseur']





) ?&gt;





&lt;?= &#036;form-&gt;field(&#036;modelPurchases, 'purchase_status')-&gt;textInput() ?&gt;








&#60;&#33;--purchase item information--&#62;      





  &lt;div class=&quot;panel panel-default&quot;&gt;


    &lt;div class=&quot;panel-heading&quot;&gt;&lt;h4&gt;&lt;i class=&quot;glyphicon glyphicon-envelope&quot;&gt;&lt;/i&gt; Contenu Achat&lt;/h4&gt;&lt;/div&gt;


    &lt;div class=&quot;panel-body&quot;&gt;


         &lt;?php DynamicFormWidget::begin([


            'widgetContainer' =&gt; 'dynamicform_wrapper', // required: only alphanumeric characters plus &quot;_&quot; [A-Za-z0-9_]


            'widgetBody' =&gt; '.container-items', // required: css class selector


            'widgetItem' =&gt; '.item', // required: css class


            'limit' =&gt; 6, // the maximum times, an element can be cloned (default 999)


            'min' =&gt; 1, // 0 or 1 (default 1)


            'insertButton' =&gt; '.add-item', // css class


            'deleteButton' =&gt; '.remove-item', // css class


            'model' =&gt; &#036;modelsPurchaseItems[0],


            'formId' =&gt; 'dynamic-form',


            'formFields' =&gt; [


                //'purchase_id',


                'item_id',


                'qte',


            


            ],


        ]); ?&gt;


        &lt;div class=&quot;container-items&quot;&gt;&#60;&#33;-- widgetContainer --&#62;


        &lt;?php foreach (&#036;modelsPurchaseItems as &#036;i =&gt; &#036;modelPurchaseItem): ?&gt;


            &lt;div class=&quot;item panel panel-default&quot;&gt;&#60;&#33;-- widgetBody --&#62;


                &lt;div class=&quot;panel-heading&quot;&gt;


                    &lt;h3 class=&quot;panel-title pull-left&quot;&gt;Contenu Achat&lt;/h3&gt;


                    &lt;div class=&quot;pull-right&quot;&gt;


                        &lt;button type=&quot;button&quot; class=&quot;add-item btn btn-success btn-xs&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-plus&quot;&gt;&lt;/i&gt;&lt;/button&gt;


                        &lt;button type=&quot;button&quot; class=&quot;remove-item btn btn-danger btn-xs&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-minus&quot;&gt;&lt;/i&gt;&lt;/button&gt;


                    &lt;/div&gt;


                    &lt;div class=&quot;clearfix&quot;&gt;&lt;/div&gt;


                &lt;/div&gt;


                &lt;div class=&quot;panel-body&quot;&gt;


                    &lt;?php


                        // necessary for update action.


                        if (&#33; &#036;modelPurchaseItem-&gt;isNewRecord) {


                            echo Html::activeHiddenInput(&#036;modelPurchaseItem, &quot;[{&#036;i}]id&quot;);


                        }


                    ?&gt;


                    &lt;div class=&quot;row&quot;&gt;


                       &lt;div class=&quot;col-sm-6&quot;&gt;


                         &lt;?= &#036;form-&gt;field(&#036;modelPurchaseItem, &quot;[{&#036;i}]item_id&quot;)-&gt;dropDownList(


       ArrayHelper::map(items::find()-&gt;all(),'item_id','item_name'),['prompt'=&gt;'Select Articles']





   ) ?&gt;


                       &lt;/div&gt;


                       &lt;div class=&quot;col-sm-6&quot;&gt;


                         &lt;?= &#036;form-&gt;field(&#036;modelPurchaseItem, &quot;[{&#036;i}]qte&quot;)-&gt;textInput(['maxlength' =&gt; true]) ?&gt;


                       &lt;/div&gt;


                    &lt;/div&gt;&#60;&#33;-- .row --&#62;


                &lt;/div&gt;


             &lt;/div&gt;


        &lt;?php endforeach; ?&gt;


        &lt;/div&gt;


        &lt;?php DynamicFormWidget::end(); ?&gt;


    &lt;/div&gt;


&lt;/div&gt;





&lt;div class=&quot;form-group&quot;&gt;


   &lt;?= Html::submitButton(&#036;modelPurchaseItem-&gt;isNewRecord ? 'Create' : 'Update', ['class' =&gt; 'btn btn-primary']) ?&gt; 


&lt;/div&gt;


&lt;?php ActiveForm::end(); ?&gt;

</div>[/html]


 PurchasesController.php

<?php


namespace backend\controllers;


use Yii;

use backend\models\Model;

use backend\models\Purchases;

use backend\models\PurchasesItems;

//use backend\models\Items;


use yii\data\ActiveDataProvider;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;

use yii\widgets\ActiveForm;

use yii\web\Response;

use yii\helpers\ArrayHelper;




/**

 * PurchasesController implements the CRUD actions for Purchases model.

 */

class PurchasesController extends Controller

{

    /**

     * {@inheritdoc}

     */

    public function behaviors()

    {

        return [

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'delete' => ['POST'],

                ],

            ],

        ];

    }


    /**

     * Lists all Purchases models.

     * @return mixed

     */

    public function actionIndex()

    {

        $dataProvider = new ActiveDataProvider([

            'query' => Purchases::find(),

        ]);


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

            'dataProvider' => $dataProvider,

        ]);

    }


    /**

     * Displays a single Purchases model.

     * @param integer $id

     * @return mixed

     * @throws NotFoundHttpException if the model cannot be found

     */

    public function actionView($id)

    {

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

            'model' => $this->findModel($id),

        ]);

    }


    /**

     * Creates a new Purchases model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     * @return mixed

     */

    /*public function actionCreate()

    {

        $model = new Purchases();

        $modelPurchaseItem = new PurchasesItems();


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

            $model->save();

            $modelPurchaseItem -> purchase_id = $model->purchase_id;

            $modelPurchaseItem -> save();

            

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

        }


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

            'model' => $model,

            'purchaseItem' => $modelPurchaseItem,

        ]);

    }*/

    

     public function actionCreate()

    {

        $modelPurchases = new Purchases();

        $modelsPurchaseItems = [new PurchasesItems];

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


            $modelsPurchaseItems = Model::createMultiple(PurchasesItems::classname());

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




            // validate all models

            $valid = $modelPurchases->validate();

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

            

            if ($valid) {

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

                try {

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

                        foreach ($modelsPurchaseItems as $modelPurchaseItem) {

                            $modelPurchaseItem->purchase_id = $modelPurchases->id;

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

                                $transaction->rollBack();

                                break;

                            }

                        }

                    }

                    if ($flag) {

                        $transaction->commit();

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

                       

                    }

                } catch (Exception $e) {

                    $transaction->rollBack();

                }

            }

       }

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

            'modelPurchases' => $modelPurchases,

            'modelsPurchaseItems' => (empty($modelsPurchaseItems)) ? [new PurchasesItems] : $modelsPurchaseItems

        ]);

    }




    /**

     * Updates an existing Purchases model.

     * If update is successful, the browser will be redirected to the 'view' page.

     * @param integer $id

     * @return mixed

     * @throws NotFoundHttpException if the model cannot be found

     */

    public function actionUpdate($id)

    {

        $model = $this->findModel($id);


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

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

        }


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

            'model' => $model,

        ]);

    }


    /**

     * Deletes an existing Purchases model.

     * If deletion is successful, the browser will be redirected to the 'index' page.

     * @param integer $id

     * @return mixed

     * @throws NotFoundHttpException if the model cannot be found

     */

    public function actionDelete($id)

    {

        $this->findModel($id)->delete();


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

    }


    /**

     * Finds the Purchases model based on its primary key value.

     * If the model is not found, a 404 HTTP exception will be thrown.

     * @param integer $id

     * @return Purchases the loaded model

     * @throws NotFoundHttpException if the model cannot be found

     */

    protected function findModel($id)

    {

        if (($model = Purchases::findOne($id)) !== null) {

            return $model;

        }


        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));

    }

}

[html]create.php

<?php

use yii\helpers\Html;

/* @var $this yii\web\View */

/* @var $model backend\models\Purchases */

$this->title = Yii::t(‘app’, ‘Creer Achat’);

$this->params[‘breadcrumbs’][] = [‘label’ => Yii::t(‘app’, ‘Achat’), ‘url’ => [‘index’]];

$this->params[‘breadcrumbs’][] = $this->title;

?>

<div class="purchases-create">

&lt;h1&gt;&lt;?= Html::encode(&#036;this-&gt;title) ?&gt;&lt;/h1&gt;





&lt;?= &#036;this-&gt;render('_form', [


    'modelPurchases' =&gt; &#036;modelPurchases,


    'modelsPurchaseItems' =&gt; &#036;modelsPurchaseItems,


]) ?&gt;

</div>[/html]


Model.php


<?php


namespace backend\models;


use Yii;

use yii\helpers\ArrayHelper;


class Model extends \yii\base\Model

{

    /**

     * Creates and populates a set of models.

     *

     * @param string $modelClass

     * @param array $multipleModels

     * @return array

     */

    public static function createMultiple($modelClass, $multipleModels = [])

    {

        $model    = new $modelClass;

        $formName = $model->formName();

        $post     = Yii::$app->request->post($formName);

        $models   = [];


        if (! empty($multipleModels)) {

            $keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));

            $multipleModels = array_combine($keys, $multipleModels);

        }


        if ($post && is_array($post)) {

            foreach ($post as $i => $item) {

                if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {

                    $models[] = $multipleModels[$item['id']];

                } else {

                    $models[] = new $modelClass;

                }

            }

        }


        unset($model, $formName, $post);


        return $models;

    }

}

Sorry for bad English!

Thanks!

hi, I find my error it works fine. But how can I do that:

what I want to do is when user create a purchases, he will be able to create purchases_items at same, and after that update column stock from table items by incrementing with purchases_items’s qte value of corresponding item_id .

NB: column stock is the stock level of items. The project is about stock management.