yii2-dynamicform icon indicating copy to clipboard operation
yii2-dynamicform copied to clipboard

Add button not working

Open saranvarma opened this issue 9 years ago • 6 comments

even though when i clicked on add button it is not adding another div . just nothing happenes . and when i open create page with out submitting every field shows the validation error . please help me

saranvarma avatar Sep 05 '15 19:09 saranvarma

Controller :

public function actionCreate() {

    $model = new Sales();

    $modelssalesproducts = [new SalesProducts()];

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

        $modelssalesproducts = DynamicForms::createMultiple(DynamicForms::classname());
        print_r($modelssalesproducts);exit;
        DynamicForms::loadMultiple($modelssalesproducts, Yii::$app->request->post());

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


}
            // validate all models
        $valid = $model->validate();
        $valid = DynamicForms::validateMultiple($modelssalesproducts) && $valid;

        if ($valid) {
            $transaction = \Yii::$app->db->beginTransaction();
            try {
                if ($flag = $model->save(false)) {
                    foreach ($modelssalesproducts as $modelsalesproducts) {
                        $modelsalesproducts->sale_id = $model->id;
                        if (! ($flag = $modelsalesproducts->save(false))) {
                            $transaction->rollBack();
                            break;
                        }
                    }
                }
                if ($flag) {
                    $transaction->commit();
                    return $this->redirect(['view', 'id' => $model->id]);
                }
            } catch (Exception $e) {
                $transaction->rollBack();
            }
        }

// echo "

";print_r($modelssalesproducts[0]);exit;
return $this->render('create', [
'model' => $model,
'modelssalesproducts' => (empty($modelssalesproducts)) ? [new SalesProducts] : $modelssalesproducts
]);
}

View :

Products

    <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' => $modelssalesproducts[0],
            'formId' => 'dynamic-form',
            'formFields' => [
                'parent_product',
                'product',
                'size',
                'quantity',
                'each_price',
                'total',
            ],
        ]); ?>

        <div class="container-items"><!-- widgetContainer -->
        <?php foreach ($modelssalesproducts as $i => $modelsalesproducts): ?>
            <div class="item panel panel-default"><!-- widgetBody -->
                <div class="panel-heading">
                    <h3 class="panel-title pull-left">Products</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 (! $modelsalesproducts->isNewRecord) {
                            echo Html::activeHiddenInput($modelsalesproducts, "[{$i}]id");
                        }
                    ?>
                    <?= $form->field($modelsalesproducts, "[{$i}]parent_product")->dropDownList($parent_items,['prompt'=>'-Choose Parent Product-']) ?>
                    <div class="row">
                        <div class="col-sm-6">
                            <?= $form->field($modelsalesproducts, "[{$i}]product")->dropDownList($products,['prompt'=>'-Choose a Product-'])?>
                        </div>
                        <div class="col-sm-6">
                            <?= $form->field($modelsalesproducts, "[{$i}]size")->textInput(['maxlength' => true]) ?>
                        </div>
                    </div><!-- .row -->
                    <div class="row">
                        <div class="col-sm-4">
                            <?= $form->field($modelsalesproducts, "[{$i}]quantity")->textInput() ?>
                        </div>
                        <div class="col-sm-4">
                            <?= $form->field($modelsalesproducts, "[{$i}]each_price")->textInput() ?>
                        </div>
                        <div class="col-sm-4">
                            <?= $form->field($modelsalesproducts, "[{$i}]total")->textInput() ?>
                        </div>
                    </div><!-- .row -->
                </div>
            </div>
        <?php endforeach; ?>
        </div>
        <?php DynamicFormWidget::end(); ?>
    </div>
</div>

saranvarma avatar Sep 05 '15 19:09 saranvarma

I have same problems.. but i have solved this. Dont forget to add

 'id' => 'dynamic-form' 
in ActiveForm :
$form = ActiveForm::begin(['id' => 'ISSUE-form']);

and in DynamicActiveWidget :

DynamicFormWidget::begin([
...
'formId' => 'ISSUE-form', //same as your ActiveForm id    
...
]);

antariksa avatar Nov 05 '15 01:11 antariksa

I read this on many sites, but why don't it work for me ? I've correctly entered both id's, then where would be the problem ?

tusharwalzade216 avatar Jan 18 '16 07:01 tusharwalzade216

Ensure that all 'div's are properly closed and there are no extra '/div's in the form.

suryasahu avatar Apr 02 '16 22:04 suryasahu

@suryasahu Thanks for hint... Struggling weeks after realized there are extra div on my form, solved my problem

moysoft avatar Jun 21 '19 00:06 moysoft

had to use

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

form

  <?php DynamicFormWidget::begin([
                'formId' => 'dynamic-form',

Specifically changing the id to dynamic-form works

mmutinda avatar Nov 19 '23 03:11 mmutinda