Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded does not work correctly #356

Open
RodrigoDornelles opened this issue Aug 16, 2021 · 10 comments
Open

Embedded does not work correctly #356

RodrigoDornelles opened this issue Aug 16, 2021 · 10 comments

Comments

@RodrigoDornelles
Copy link

The Field even works visually, but it puts "name" attributes in the html meaningless which leads to an incorrect request not being able to save correctly.

expected

<input type="text" id="formulario-1-campos-opcoes-0" class="form-control is-valid" name="Formulario[campos][1][opcoes][0]" aria-invalid="false">

happen

<input type="text" id="formulario-campos-1-opcoes-campos-1-opcoes-0" class="form-control is-valid" name="Formulario[campos][1][opcoes][campos[1][opcoes]][0]" aria-invalid="false">
  • name occurred Formulario[campos][1][opcoes][campos[1][opcoes]][0] should be Formulario[campos][1][opcoes][0]
  • id occurred formulario-campos-1-opcoes-campos-1-opcoes-0 should be formulario-1-campos-opcoes-0

NOTE

thinking now, I believe it's a conflict with the activeform mayable

@RodrigoDornelles
Copy link
Author

RodrigoDornelles commented Aug 17, 2021

Note

I did some tests, and realized that the "name" attribute is set correctly when used in columns, but only in a new model, when you go to edit another problem happens.

When create a new model

Works

echo MultipleInput::widget([
    'model' => $model,
    'attribute' => 'campos',
    'columns' => [
        [
            'name' => 'pergunta',
            'type' => 'textarea',
        ],
        [
            'name' => 'opcoes',
            'type'  => MultipleInput::className(),
            'options' => [
                'columns' => [
                    [
                        'name' => 'opcao'
                    ]
                ]
            ]
        ]
    ],
])?>

Related problem

echo MultipleInput::widget([
    'model' => $model,
    'attribute' => 'campos',
    'columns' => [
        [
            'name' => 'pergunta',
            'type' => 'textarea',
        ],
        [
            'name' => 'opcoes',
            'type'  => MultipleInput::className(),
        ]
    ],
])?>
  • name occurred Formulario[campos][1][opcoes][campos[1][opcoes]][0] should be Formulario[campos][1][opcoes][0]
  • id occurred formulario-campos-1-opcoes-campos-1-opcoes-0 should be formulario-1-campos-opcoes-0

When update model

Another problem

echo MultipleInput::widget([
    'model' => $model,
    'attribute' => 'campos',
    'columns' => [
        [
            'name' => 'pergunta',
            'type' => 'textarea',
        ],
        [
            'name' => 'opcoes',
            'type'  => MultipleInput::className(),
            'options' => [
                'columns' => [
                    [
                        'name' => 'opcao'
                    ]
                ]
            ]
        ]
    ],
])?>
  • name occurred Formulario[campos][0][opcoes][opcao][opcao] should be Formulario[campos][0][opcoes][0][item]
  • id occurred formulario-campos-0-opcoes-item-item should be formulario-campos-0-opcoes-0-item

Related problem

echo MultipleInput::widget([
    'model' => $model,
    'attribute' => 'campos',
    'columns' => [
        [
            'name' => 'pergunta',
            'type' => 'textarea',
        ],
        [
            'name' => 'opcoes',
            'type'  => MultipleInput::className(),
        ]
    ],
])?>
  • name occurred Formulario[campos][1][opcoes][campos[1][opcoes]][0] should be Formulario[campos][1][opcoes][0]
  • id occurred formulario-campos-1-opcoes-campos-1-opcoes-0 should be formulario-1-campos-opcoes-0

@unclead
Copy link
Owner

unclead commented Aug 17, 2021

@RodrigoDornelles Hi, thx for the reproduce case. What version of the widget do you use?

@RodrigoDornelles
Copy link
Author

Hi, thx for the reproduce case. What version of the widget do you use?

@unclead see

packge version
Yii2 2.0.42.1
Jquery 3.5.1
Plugin 2.24.0

@unclead
Copy link
Owner

unclead commented Aug 18, 2021

@RodrigoDornelles I can't reproduce it without seeing the code of the model.

@RodrigoDornelles
Copy link
Author

I can't reproduce it without seeing the code of the model.

@unclead here

<?php

namespace common\models;

use Yii;
use yii\helpers\Json;

class Formulario extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'formulario';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            //...
            [['campos'], 'safe'],
            [['email_copias'], 'each', 'rule' => ['email']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function beforeSave($insert)
    {
        if (!parent::beforeSave($insert)) {
            return false;
        }

        $this->email_copias = Json::encode($this->email_copias);
        $this->campos = Json::encode($this->campos);
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function afterFind()
    {
        parent::afterFind();
        $this->email_copias = Json::decode($this->email_copias, true);
        $this->campos = Json::decode($this->campos, true);
    }
 }

@RodrigoDornelles
Copy link
Author

@unclead I did a hardcoded interim fix, this is sufficient for my specific case, but it could be a problem for other cases.

'value' => fn ($data) => isset($data['opcoes'])? $data['opcoes']['item']: null,

@unclead
Copy link
Owner

unclead commented Sep 3, 2021

TBH I didn't manage to reproduce the issue so I can't say why it happens. Did you try to debug it? I mean go to the widget's code and try to figure out the root cause?

@RodrigoDornelles
Copy link
Author

TBH I didn't manage to reproduce the issue so I can't say why it happens. Did you try to debug it? I mean go to the widget's code and try to figure out the root cause?

I tried debugging and solving, but i found the plugin much more complex than thought.

I'll still work hard on a system, notice anything, or if I can find the problem.

@unclead
Copy link
Owner

unclead commented Sep 3, 2021

but i found the plugin much more complex than thought.

That's true. The result of years of incremental improvements. I'm working on refactoring it's architecture right now and it's quite complicated as well (even considering I did most of the features 😄 )

@bax234
Copy link

bax234 commented May 1, 2024

The Field even works visually, but it puts "name" attributes in the html meaningless which leads to an incorrect request not being able to save correctly.

expected

<input type="text" id="formulario-1-campos-opcoes-0" class="form-control is-valid" name="Formulario[campos][1][opcoes][0]" aria-invalid="false">

happen

<input type="text" id="formulario-campos-1-opcoes-campos-1-opcoes-0" class="form-control is-valid" name="Formulario[campos][1][opcoes][campos[1][opcoes]][0]" aria-invalid="false">
* **name** occurred `Formulario[campos][1][opcoes][campos[1][opcoes]][0]` should be `Formulario[campos][1][opcoes][0]`

* **id** occurred `formulario-campos-1-opcoes-campos-1-opcoes-0` should be `formulario-1-campos-opcoes-0`

NOTE

thinking now, I believe it's a conflict with the activeform mayable

in my case i've had trying workaround to make custom generate element DOM id/name attribute in afterInit and afterAddRow JS event with specific array value element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants