0

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.

4
  • Please put your DynamicFormWidget code Commented Sep 6, 2016 at 13:12
  • Are there any dynamicform_hashed value in your html head tag in script tag? Commented Sep 6, 2016 at 13:57
  • yes there is a dynamicform_hashed value defined in javascript tags in DynamicFormWidget class Commented Sep 6, 2016 at 15:49
  • if you still have the problem check this link stackoverflow.com/a/41159047/7973949 Commented Jun 11, 2017 at 20:35

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.