This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi! I am trying to update a model using a custom popup form. I have used the example from the tutorial page, but i get a error when i am trying to use the Record finder to change the entry. Here is the code i am using:
public function onLoadCreateUpdateForm(){
$model = Ticket::findOrFail(post('ticketId'));
$this->vars['model'] = $model;
$config = $this->makeConfig('$/db/apollo/models/ticket/fields.yaml');
$config->alias = 'updateForm';
$config->arrayName = 'Ticket';
$config->model = $model;
$widget = $this->makeWidget('Backend\Widgets\Form', $config);
$widget->bindToController();
$this->vars['widget'] = $widget;
return $this->makePartial('form_update_ticket');
}
field.yaml:
device:
label: 'Device model'
oc.commentPosition: ''
nameFrom: name
descriptionFrom: description
list: $/db/apollo/models/device/columns.yaml
span: full
type: recordfinder
tab: Basic
Error: "A widget with class name 'updateFormDevice' has not been bound to the controller" on line 517 of /modules/backend/Classes/Controller.php"
I had this issue as well. You need to prepare and bind the widget in an earlier stage of the lifecycle. In your case, this means the widget needs to be prepared in the Controllers constructor.
It sounds like a good idea to refactor your code and create a method that simply initializes a widget instance, as seen here: https://github.com/responsiv/pay-plugin/blob/master/controllers/Invoices.php#L69-L76
Then, in your constructor call that method and it should work. However, this will always, i.e. on every page load for that given controller, call this method, even if the modal is not opened. So one more efficient solution would be to check if some POST input is present and call the method based on that check.
Hi, i have refactored the code so i instantiate the widget in the constructor, but still get the same error:
public function onLoadCreateUpdateForm(){
$this->vars['model'] = $this->ticket;
$this->vars['widget'] = $this->updateFormWidget;
return $this->makePartial('form_update_ticket');
}
protected function createUpdateFormWidget(){
$config = $this->makeConfig('$/db/apollo/models/ticket/fields.yaml');
$config->alias = 'updateForm';
$config->arrayName = 'Ticket';
$config->model = $this->ticket;
$widget = $this->makeWidget('Backend\Widgets\Form', $config);
$widget->bindToController();
return $widget;
}
protected $updateFormWidget;
protected $ticket;
public function __construct()
{
parent::__construct();
BackendMenu::setContext('DB.Apollo', 'main-menu-item', 'side-menu-item4');
if($ticketId = post('ticketId')){
$this->ticket = Ticket::findOrFail($ticketId);
$this->updateFormWidget = $this->createUpdateFormWidget();
}
}
1-4 of 4