D
D
Dmitry2019-03-27 14:36:30
Yii
Dmitry, 2019-03-27 14:36:30

How to pass post id to widget?

Good evening.
Tell me, please, how to do it right.
There is a submission form. Displayed in a modal window. The button for calling a modal window with a form for a new ad is located in layouts/main.php. The button for calling the window with the form for editing is located in another modal window.
It is necessary to make it possible to display the form not only for submitting a new ad, but also for editing.
Actually, this is a rework of the existing code. But the old code, I think, is not very good, to put it mildly.
Now, in order to get the model for editing, an ajax request is sent to the controller, the response comes with data that is parsed using jquery and substituted into the form. But the form is very voluminous, a lot of extra code is obtained.
I decided to transfer all this to a widget in order to display either an empty model instance or a filled one directly in the widget, for editing, thereby reducing the amount of code and removing unnecessary requests to controllers.
I coped with the first part without any problems))) I
created a widget and connected the view with the form.

class SaleForm extends Widget
{
    public $model;
    public $files;
    public function init()
    {
        $this->model = new Cars;
        $this->files = new Images;
    }

    public function run()
    {
        return $this->render('index', ['model' => $this->model, 'files' => $this->files]);
    }
}

But now I don’t know how to transfer the ad id to the widget.
I would like to get something similar.
public function init()
    {
        if($this->id == null){
            $this->model = new Cars;
            $this->files = new Images;           
        }
        else{
            // тут получать экземпляр необходимой модели 
        }
    }

I know that passing a parameter to a widget is not a big problem. It is not a problem to get the id itself when clicking on the "edit" button. But now I don’t understand how to transfer this parameter to the widget, I don’t know. Tell me how to do it right? ps You can still send the same ajax id to the controller action and return the form view with the filled model passed to it. But still I wonder if it is possible to do without it?
<?= SaleForm::widget(['id' => $id])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2019-03-27
@slo_nik

or have a custom modal for each button, as implemented in https://www.yiiframework.com/extension/yiisoft/yii...
or receive an ajax form
or have a form template (for example, https://learn.javascript.ru /template-tag) and using js to substitute id into the template. More precisely, build html based on the template and the resulting id. while id can be the data attribute of the button.
offtop, here it is

public $model;
public $files;
public function init()
    {
        $this->model = new Cars;
        $this->files = new Images;
    }

meaningless. It is necessary either to check for emptiness before redefining or to make the properties not public.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question