Answer the question
In order to leave comments, you need to log in
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]);
}
}
public function init()
{
if($this->id == null){
$this->model = new Cars;
$this->files = new Images;
}
else{
// тут получать экземпляр необходимой модели
}
}
'id' => $id])
= SaleForm::widget([Answer the question
In order to leave comments, you need to log in
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;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question