How to pass the record id in the widget?

Good evening.
Can you please tell me how to do it.
Is a form of advertising. Is displayed in a modal window. A button to call a modal window with form for the new ads located in layouts/main.php. Button to open form to edit is in another modal window.
You need to make the ability to take the form not only for submitting a new ad, but editing.
In fact, this reworking of the existing code. But the old code I think is not very good, to put it mildly.
Now to get a model for editing, an ajax request is sent to the controller, in response to the coming data with the help of jquery parsed and inserted in the form. But the form is very large, a lot of extra code work.
Decided to move all of this in a widget to right in the widget display, or an empty model instance, or filled, for editing, thereby reducing code size and remove unnecessary requests to the controllers.
In the first part I have done without problems)))
Have created a widget hooked up 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 how now to pass the ad id in the widget does not know.
I would like to get something similar.
public function init()
{
 if($this->id == null){
 $this->model = new Cars;
 $this->files = new Images; 
}
else{
 // here to copy the required models 
}
 }

I know that to pass a parameter to a widget not a big problem is
<?= SaleForm::widget(['id' => $id])
No problems is to get the id when you click on the "edit"button.
But as it is now this option to pass in the widget don't understand or know.
Tell me how to do?

p.s.
You can all the same ajax to send id to controller action and return the form to sent him the completed model.
But still wondering whether it is possible to do without it?
March 19th 20 at 08:50
1 answer
March 19th 20 at 08:52
Solution
or have each button its modelku, as implemented in https://www.yiiframework.com/extension/yiisoft/yii...
or get form ajax
or have the form template (e.g., https://learn.javascript.ru/template-tag) and means js to substitute the id in the template. Rather build the html template and the received id. with this id, it can be a data attribute of the button.

Oftop, here it is
public $model;
public $files;
public function init()
{
 $this->model = new Cars;
 $this->files = new Images;
 }

doesn't make sense. It is necessary, or before redefining the check on void or to make the properties public.
That is, in each module already loaded the form with the fields filled in for editing?
Now using ajax to get the form.
Substitute the id in the form Yes, no problem, but as with the rest of the data? - richmond_Bogisich commented on March 19th 20 at 08:55
@richmond_Bogisich,
but as with the rest of the data?

depends how many there are. Can ajax take, can through date attributes, you can form the json and stick it in a variable, then pull it in the formation of the forms, pulling in the id from the json config for all fields. - catharine_Hettinger commented on March 19th 20 at 08:58
@catharine_Hettinger, in any case, without queries to the database cannot do?
Maybe then there is no point to move the form in a widget?
It turns out that if you make a widget, it is useful only in order to call the form when you click "submit ad". When editing will still need to send an ajax request to the server. - richmond_Bogisich commented on March 19th 20 at 09:01
@richmond_Bogisich,
in any case, without queries to the database cannot do?

why? you can do

Maybe then there is no point to move the form in a widget?

it is not excluded. The transfer makes sense for re-use with parameterization. If this is not - it makes no sense.

When editing will still need to send an ajax request to the server.

don't understand what's ajax? Can Ajax and a widget to send. Pass it the url, date, attributes, or parameter of the widget and it's there something is opening, something is sending etc, what would he base been otherwise, it is not mvc. - catharine_Hettinger commented on March 19th 20 at 09:04
@catharine_Hettinger, the fact that in layouts-e a bunch of forms, and other html, you should bring it all in order.
Yes, the "create" button should be on all pages, they are few, but still.
Might be better to do as suggested hereand the files for the form just put in a separate folder in the frontend/views... - richmond_Bogisich commented on March 19th 20 at 09:07
@richmond_Bogisich,
the fact that in layouts-e a bunch of forms, and other html, you should bring it all in order.

there is a separate file, why are the widgets? - catharine_Hettinger commented on March 19th 20 at 09:10
@richmond_Bogisich, by the way, there is still https://www.yiiframework.com/doc/api/2.0/yii-widge... and they often are a great help when refactoring, but I know about them and use them for units, and the thing is comfortable.
And there ContentDecorator: https://www.yiiframework.com/doc/api/2.0/yii-widge... (it allows to save the basic template from a heap of html overtok) - catharine_Hettinger commented on March 19th 20 at 09:13
@catharine_Hettinger, I guess it'll do. All forms to separate files, sort directories, to make it easier to find.
To create a separate controller for handling forms. Or it will be too much? - richmond_Bogisich commented on March 19th 20 at 09:16
@catharine_Hettinger, I read about it, but had not yet applied anywhere. must still be read, perhaps as a variant of solving the problem. - richmond_Bogisich commented on March 19th 20 at 09:19
@catharine_Hettinger, will have to see application examples)))
Do You happen to have any in the channel?))) - richmond_Bogisich commented on March 19th 20 at 09:22
@richmond_Bogisich, no, the way the topic will be interesting. I'm in the projects that I came across never seen any Block s no ContentDecorator-s. It is necessary to own be free. About the use and differences Block + ContentDecorator + Widget. As things at first used for the same things, but no. For something entirely different. - catharine_Hettinger commented on March 19th 20 at 09:25
@catharine_Hettinger, that I have not seen anywhere else. Although I tried to apply, but for some reason abandoned the idea. Why - I do not remember. - richmond_Bogisich commented on March 19th 20 at 09:28
@catharine_Hettinger, but in the documentation, as always, like a cat naplakal( - richmond_Bogisich commented on March 19th 20 at 09:31
@catharine_Hettinger, came up with the idea.
Whatever it was, but I have to call the same form. In the first case, an empty, another full.
To call empty there is a button that hangs in layout-e, and the second hangs in a modal window, which shows all ads of the user.
So still a widget.
The parameters I'm passing what should be the button and the second argument I need to pass the ad id or null, if this widget is invoked in the layout-e.
In the widget to put the form itself. In init() to check for id if there is, give the completed form, if not empty.
And calling the widget comes down to this option:
// called layout-e, to print a blank form
echo SaleForm::widget([
 'type' => 'create', // the type of/the appearance of the button
 'id' => null // or not write anything at all, and in the widget to check for null
])

// call another modal that displays all ad user
echo SaleForm::widget([
 'type' => 'update', // the type of/the appearance of the button
 'id' => $model->id // messages are displayed in a loop
])

In the view which is rendered in run() to check type and based on that render button. Modal window with form is also in the widget.
But how to be with a record and update into the database? The controller in the widget would like as not allowed... the Third option with the route to the correct action in a third-party controller? Or what?
What do you say about this implementation? - richmond_Bogisich commented on March 19th 20 at 09:34

Find more questions by tags Yii