JToolBarHelper::apply() problem

67 views
Skip to first unread message

becyn82

unread,
Mar 12, 2017, 7:00:19 PM3/12/17
to Joomla! General Development
i use this backend save button with a model.controller argument.
One of the first strange things that happened was that it didn't like the method name "saveform". It refused to call the method!
Changing the name to saveit which made it happy. ( can anybody shed some light on this?)

2) Now it calls the method but dumps the browser in .administrator/index.php after running the method while leaving the form displayed ( which is the the expected behaviour). However the component has now lost control and none of the buttons will work afterwards.
I have the option and view in hidden fields on the form but they are ignored when I use the save button..

Couldn't find much on JToolBarHelper::apply() other tha you can give it an argument for the controller and method.
Does it need some other undocumented argument to return control to the component?






Viper

unread,
Mar 13, 2017, 3:26:23 AM3/13/17
to Joomla! General Development
If you need a custom behaviour for apply button, implement it in controller. Example:
button:
JToolbarHelper::apply('mars.apply');

will call apply method in you mars controller. So in this method you must to implement all you code.
Iа you want to return into form just use $this->setRedirect('url');

See as example https://github.com/Globulopolis/kinoarhiv/blob/3.1.x-dev/administrator/components/com_kinoarhiv/controllers/awards.php#L90
In this method I check session token, access rules, validate the form($model->validate()), store temporary edited data in session, and redirect user to an url based on task($this->getTask() will get a method name from 'controller.method').

PS! Calling model method directly from web is a bad code.
Message has been deleted

becyn82

unread,
Mar 13, 2017, 12:22:58 PM3/13/17
to Joomla! General Development
yes the model.controller was a typo. I meant controller.method.

 the ticket controller method calls the ticket model method and executes then goes to the the default view, but that ends the component's actions. nothing works after that because the browser lands at adminstrator /index.php for some reason. If the controller and view are of the same name I didn't think that any specific redirect are neccessary.

However I did try doing that with set ('view', 'ticket'0 and also by using the long URL version , but with redirect(JRoute::_(...)
I used Parent::display insead of return.

Viper

unread,
Mar 13, 2017, 2:50:26 PM3/13/17
to Joomla! General Development
How you instantiate you component? Via JControllerLegacy::getInstance(), execute(), redirect() ? And that you have in you main controller::display() method? Looks like you are missing something.

becyn82

unread,
Mar 13, 2017, 6:43:24 PM3/13/17
to Joomla! General Development
This works with a special search screen that comes up with a different view , once a ticket is selected from the list, it switches to the ticket view. This all works fine, except when I attempt to use the Joomla save button is when it 'gets lost' .

There is a an instanciation  file and master controller in the component root directory. each view has it's own mode and controller as well.

I am using JcontrollerLegacy for the master controller and JControllerForm for the view controllers. I don't think this an issue since it's just another extended version of the base controller anyway.

The controllers have parent::display at the bottom.

I noticed that you don't use the display() method but a redirect and return instead. Is there there some special reason for that?

Viper

unread,
Mar 14, 2017, 4:26:51 AM3/14/17
to Joomla! General Development
Do you have a hidden input with task in you form?
<input type="hidden" name="task" value=""/>

required if you want to send a task variable to the server, because button just submit the form with selected task and do not alter any fields or sended variables by default.

I'm do not use a diplay() method in subcontrollers because main controller have this method. Using custom display() method is not needed in most of situations.

becyn82

unread,
Mar 15, 2017, 10:52:09 AM3/15/17
to Joomla! General Development
Yes, I have all three, option,view and task in the hidden input tags. The value of the view and component have the names filled in, but task is value="". The blank task value is over-witten by the button argument,as  I understand it.
It does do the post task, but does not return control to the component. This is what baffles me.

becyn82

unread,
Mar 15, 2017, 11:50:54 AM3/15/17
to Joomla! General Development
I moved on from this a few days to get my mind out of the rut it was in.
Within 15 minutes after returning to the issue the solution presented itself.

1) I ditched the nested JROUTE within the redirect. ( i've read that it is not reliable in the backend?)
2) used the JFactory::getApplication()->redirect instead of the the controller method $this-> redirect().
( I'm not sure if this was part of the problem. It doesn't seem that it should be...)
3) added the current itemid to return to  the form that was just saved.

JFactory::getApplication()->redirect('index.php?option=com_mcforms&view=ticket&itemid='.$id);                
return;

Ihe item id was an obvious omission, but the the main thing was JROUTE was apparently getting lost. Even without the itemid i should have gotten back a "live" blank form.
Everything looks and works as expected now.

Thanks!

Viper

unread,
Mar 16, 2017, 2:07:35 AM3/16/17
to Joomla! General Development
See my second post. I told you about $this->setRedirect().
1. Yes. It's not needed in backend.
2. $this->redirect($url), $this->redirect(), $this->setRedirect($url), JFactory::getApplication()->redirect($url) in controller will doing the different things. See source code for understanding how they differ.
Reply all
Reply to author
Forward
0 new messages