0

I am using wbraganca dynamic form widget. It works fine for the Create action. Let me thanks for those guys making great tutorial video on youtube!!!

I am working on the Update action now. I work it on a purchase order function.

the controller of update action :

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $modelsItem = $model->purchaseitems;

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

        $oldIDs = ArrayHelper::map($modelsItem, 'purchaseitem_id', 'purchaseitem_id');
        $modelsItem = Model::createMultiple(Purchaseitem::classname(), $modelsItem);
        Model::loadMultiple($modelsItem, Yii::$app->request->post());
        $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsItem, 'purchaseitem_id', 'purchaseitem_id')));

        $valid = $model->validate();
        $valid = Model::validateMultiple($modelsItem) && $valid;

        if ($valid) {
            $transaction = \Yii::$app->db->beginTransaction();
            try {
                if ($flag = $model->save(false)) {
                    if (! empty($deletedIDs)) {
                        Purchaseitem::deleteAll(['purchaseitem_id' => $deletedIDs]);
                    }
                    foreach ($modelsItem as $modelItem) {
                        $modelItem->purchaseitem_purchaseorder_id = $model->purchaseorder_id;
                        $modelItem->purchaseitem_description = Inventory::findOne($modelItem->purchaseitem_inventory_id)->inventory_partno;

                        if (! ($flag = $modelItem->save(false))) {
                            $transaction->rollBack();
                            break;
                        }
                    }
                }
                if ($flag) {
                    $transaction->commit();
                    return $this->redirect(['view', 'id' => $model->purchaseorder_id]);
                }
            } catch (Exception $e) {
                $transaction->rollBack();
            }
        }

        //return $this->redirect(['view', 'id' => $model->purchaseorder_id]);
    } else {
        return $this->render('update', [
            'model' => $model,
            'modelsItem' => (empty($modelsItem)) ? [new Purchaseitem] : $modelsItem

        ]);
    }
}

But I think the problem may happen on the view file, as the Select2 field can show the value, which is the 'id' of the product rather than the product code.

view:

<div class="panel panel-default"> 
    <div class="panel-body"> 
         <?php DynamicFormWidget::begin([
                'widgetContainer' => 'dynamicform_wrapper', 
                'widgetBody' => '.container-items', 
                'widgetItem' => '.item', 
                'limit' => 50, 
                'min' => 1, 
                'insertButton' => '.add-item', 
                'deleteButton' => '.remove-item', 
                'model' => $modelsItem[0],
                'formId' => 'dynamic-form',
                'formFields' => [
                    'purchaseitem_inventory_id',
                    'purchaseitem_qty',
                    'purchaseitem_cost_usd',
                    'purchaseitem_deliverydate',
                ],
            ]); ?>
        <?php foreach ($modelsItem as $i => $modelItem): ?>
            <div class="item">
                <?php
                    // necessary for update action.
                    if (! $modelItem->isNewRecord) {
                        echo Html::activeHiddenInput($modelItem, "[{$i}]purchaseitem_id");
                    }
                ?>
                <div class="row">
                    <?= $form->field($modelItem, "[{$i}]purchaseitem_inventory_id")->widget(
                            Select2::classname(), [
                                'pluginOptions' => [
                                    'allowClear' => true,
                                    'minimumInputLength' => 2,
                                    'language' => [
                                        'errorLoading' => new JsExpression("function () { return 'Error on finding results...'; }"),
                                    ],
                                    'ajax' => [
                                        'url' => Url::toRoute('inventory/inventorylist'),
                                        'dataType' => 'json',
                                        'data' => new JsExpression('function(params) { return {q:params.term}; }')
                                    ],
                                    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                    'templateResult' => new JsExpression('function(purchaseitem_inventory_id) { return purchaseitem_inventory_id.text; }'),
                                    'templateSelection' => new JsExpression('function (purchaseitem_inventory_id) { return purchaseitem_inventory_id.text; }'),
                                ],
                            ])->label(false) ?>
                    <?= $form->field($modelItem, "[{$i}]purchaseitem_qty")->textInput(['maxlength' => true])->label(false) ?>
                    <?= $form->field($modelItem, "[{$i}]purchaseitem_cost_usd")->textInput(['maxlength' => true])->label(false) ?>
                    <?= $form->field($modelItem, "[{$i}]purchaseitem_deliverydate")->widget(
                            DatePicker::className(), [
                                'options' => [
                                    ['placeholder' => 'Please enter delivery date'],
                                ],
                                'removeButton' => false,
                                'pluginOptions' => [
                                    'autoclose'=>true,
                                    'format' => 'yyyy-mm-dd',
                                    'todayHighlight' => true,
                                ]
                            ]
                        )->label(false) ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

I have a thought that the problem maybe related to that few lines of JsExpression function.

'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(purchaseitem_inventory_id) { return purchaseitem_inventory_id.text; }'),
'templateSelection' => new JsExpression('function (purchaseitem_inventory_id) { return purchaseitem_inventory_id.text; }'),

For the Select2 query URL method is here:

public function actionInventorylist($q = null, $id = null) {
    Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
    $out = ['results' => ['id' => '', 'text' => '']];
    if (!is_null($q)) {
        $query = new Query;
        $query->select('inventory_id AS id, inventory_partno AS text')
            ->from('inventory')
            ->where(['like', 'inventory_partno', $q])
            ->limit(10);
        $command = $query->createCommand();
        $data = $command->queryAll();
        $out['results'] = array_values($data);
    }
    elseif ($id > 0) {
        $out['results'] = ['id' => $id, 'text' => Inventory::find($id)->inventory_partno];
    }
    return $out;
}

I can load the record, when I click in the update view. Most of the data are feed in right place of the form, except the 'partno' field. I use Select2 to let user select partno by text and store the 'id' in table. It works on the Create view. but in the update view, it only show the 'id' instead of the 'partno'.

if I make input to the field, I can select 'other' partno only, let me explain here:

if there are 2 code, "ABC" with 'id' is 1, "XYZ" with 'id' 2.

the record original is "ABC", the field show "1". If I input "XYZ", it will show "XYZ" as normal effect of widget. But, if I change back to "ABC", it will show "1" instead of "ABC".

And the form also cannot submit for update. the button click with no effect.

I am new to Yii2 framework, and quite stuck on this issue, does anyone knows how can I solve this? THANKS!!!!

1 Answers1

0

I just solve the problem, a few issue happened actually.

To solve the Select2 widget cannot display the partno instead of the ID, I find the partno by the ID and feed it with initValueText in Select2. For my case:

$partno = empty($modelItem->purchaseitem_inventory_id) ? '':Inventory::findOne($modelItem->purchaseitem_inventory_id)->inventory_partno;
$form->field($modelItem, "[{$i}]purchaseitem_inventory_id")->widget(Select2::classname(), ['initValueText' => $partno, ...

About the update POST fail issue, I got error of Getting unknown property: backend\models\Purchaseitem::id, and I found it happened on the Dynamic-form widgets Model.php line 25. That lines is $keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));

If I change the 'id' to my ID field name, i.e 'purchaseitem_id', it will work, but this model will only work for this Id field name afterward. So I get the model's primaryKey and make it work for my other model.

add this line $primaryKey = $model->tableSchema->primaryKey; and modify above line $keys = array_keys(ArrayHelper::map($multipleModels, $primaryKey, $primaryKey));