Yii2, render ActiveForms from another model inside a ListView

250 Views Asked by At

I'm struggling to modify some data that I'm passing to a listView with an ActiveDataProvider.

This a part of my _form.php

The model here represents a Product.

_form.php

use common\models\Product


<?php $form = ActiveForm::begin();?>


<?php echo
    ListView::widget([
        'dataProvider' => new yii\data\ActiveDataProvider
        ([
            'query' => common\models\productsAdditionalInfo::find()->where(['code' => $productsArray]),
            'pagination' => false,
        ]),
        'itemView' => '_list-grammature',
    ]);

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

In my _list-grammature.php I'm basically rendering the data from productsAdditionalInfo which is also a container that took data from other tables.

_list-grammature.php

<?php

    $additionalInfoForm = ActiveForm::begin(
        [
            'id' => "form-grammatura-{$idFormAndButton}",
            'method' => 'post',
            'action' => ['backend\controllers\productsAdditionalInfoController/updateProductsAdditionalInfo'],
        ]
    );
?>

    <th colspan="4">Factory</th>
</tr>
<?php
    echo $this->render(
        'items/_factory-list',
        [
            'form' => $form,
            'forCode' => $model->code,
            'factories' => $model->factories,
        ]
    );
?>
</tbody>
<tbody>
    <tr>
        <th colspan="4">Raw Materials</th>
    </tr>
    <?php
        echo $this->render(
            'items/_raw-materials-list',
            [
                'form' => $form, 'forCode' => $model->code,
                'rawMaterials' => $model->rawMaterials,
            ]
        );

        echo Html::submitButton(
            '<i class="fas fa-cloud-upload"></i> Save',
            [
                'id' => "btn-save-{$model->firm_id}",
                'class' => 'btn btn-primary has-icon-left px-4 mx-2 elevation-5',
            ]
        )
    ?>
<?php $additionalInfoForm->end();?>

I've only took a small part of the page to give a hint of her behavior.

Basically the render part work like I expected, but when I try to modify the various textArea/input and submit the changes, the page is still bindend to the Products model and not to my ProductsAdditionalInfo model.

The queryString accordingly to what I'm saying is:

http:/.../backend/PRODUCTS/update?id=10

I don't know if I'm doing a lecit operation, I'm basically inserting in a ActiveForm, a listView which create several ActiveForms from another model, but when I try to submit one of the inner ActiveForms it updates (with no data) the model of the outer ActiveForm.

What In my mind I was thinking to get:

<form Product>

    <form1 productsAdditionalInfo>
        <button1 submit data to productAddionalInfo>
    </form1 productsAdditionalInfo>

     <form2 productsAdditionalInfo>
        <button2 submit data to productAddionalInfo>
    </form2 productsAdditionalInfo>

<Button submit Product>
</form Product>

What instead I get:

<form Product>

    <div data-key=Product1>
        <button1>
    </div data-key=Product1>

     <div data-key=Product2>
        <button2>
    </div data-key=Product2>

<Button submit Product>
</form Product>
0

There are 0 best solutions below