Search code examples
phpyii2yii2-advanced-appdynamicform

Yii2 dynamic form not adding new field on clicking Add button


I have integrated "wbraganca Yii2 dynamic form" in my Yii2 application, but when i click on Add(+) button new field does not get added and in console i get following error:

Uncaught ReferenceError: dynamicform_1b7f3e0d is not defined

Please don't mark it as duplicate as i don't find solution of this problem till now. Thanks in advance.

Here is my widget code:

<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<div class="row">
    <div class="col-sm-6">
        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
    </div>
    <div class="col-sm-6">
        <?= $form->field($model, 'note')->textInput(['maxlength' => true]) ?>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Addresses</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' => $aps[0],
            'formId' => 'dynamic-form',
            'formFields' => [
                'product_id',
                'pest_disease_id',
            ],
        ]); ?>

        <div class="container-items"><!-- widgetContainer -->
        <?php foreach ($aps as $i => $ap): ?>
            <div class="item panel panel-default"><!-- widgetBody -->
                <div class="panel-heading">
                    <h3 class="panel-title pull-left">Address</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 (! $ap->isNewRecord) {
                            echo Html::activeHiddenInput($ap, "[{$i}]id");
                        }
                    ?>

                    <div class="row">
                        <div class="col-sm-6">
                            <?= $form->field($ap, "[{$i}]product_id")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-6">
                            <?= $form->field($ap, "[{$i}]pest_disease_id")->textInput(['maxlength' => true]) ?>
                        </div>
                    </div><!-- .row -->

                </div>
            </div>
        <?php endforeach; ?>
        </div>
        <?php DynamicFormWidget::end(); ?>
    </div>
</div>

<div class="form-group">
    <?= Html::submitButton($ap->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
</div>

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

I found the problem in DynamicFormWidget class, js is not rendring at POS_HEAD position protected

protected function registerOptions($view)
{  
    $encOptions = Json::encode($this->_options);

    $this->_hashVar = DynamicFormWidget::HASH_VAR_BASE_NAME . hash('crc32', $encOptions);
    $view->registerJs("var {$this->_hashVar} = {$encOptions};\n", $view::POS_HEAD);
    //$view->registerJs("var {$this->_hashVar} = {$encOptions};\n", $view::POS_READY);
}

if i change position to POS_READY then javascript code get rendered but then remove button stops working.


Solution

  • please replace this code with your DynamicFormWidget class:

    protected function registerOptions($view)
    {  
        $encOptions = Json::encode($this->_options);
    
    $this->_hashVar = DynamicFormWidget::HASH_VAR_BASE_NAME . hash('crc32', $encOptions);
    $view->registerJs("var {$this->_hashVar} = {$encOptions};\n", $view::POS_END);
    }
    

    You need to change script positioning from POS_HEAD to POS_END