This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I have a controller which implements three behaviors: ListController, FormController, and RelationController.
There is an ajax handler in my controller. How can I access the target model when I am on the edit page? I tried using $this->formGetModel()
but it returns null.
public function onAddMeaning()
{
$targetModel = $this->formGetModel();
$sessionKey = $this->formGetSessionKey();
}
Accessing formGetSessionKey()
would also throw an exception: saying that form widget is null:
Call to a member function getSessionKey() on null
Why is my form widget not initialized?
Last updated
Hi, controllers don't have state. The controller functions are called via routes, and on each call a new controller object is created. So you need to pass model ID to the Ajax handler and then call
$this->controller->formFindModelObject($recordId);
to get the target model
Thanks. I got it working this way:
public function onAddMeaning($recordId)
{
$targetModel = $this->formFindModelObject($recordId);
}
The interesting thing is that I didn't pass any $recordId
to this function. It was provided automatically. Magic!
P.S. I found an old comment by Samuel about this (which was a reply to my comment, lol):
The ID is passed automatically, taken from the page URL, the same as a page action. Magic!
Last updated
Yes, the record id comes from the update page router from which the ajax call originates (update/:record_id)
It's briefly mentioned here: https://octobercms.com/support/article/ob-19
"In either case, the AJAX method will look at the URL for parameters, just like an action would..."
Also take a minute to look this video which talks about the MVC in OctoberCMS, it's explained at this sequence: https://youtu.be/llo6xyKx3i0?list=PLfQvk5RK_e-Zim57m_CptCXYlgFT7P_sv&t=399
[EDIT] The next time I should click the @mjauvin link before talk like a Parrot ;p
In two words: the Ajax handlers receive the same parameters that the action you are currently performing in formcontroller (update/delete will receive the recordId, create won't).
Last updated
1-7 of 7