Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Inject data to a form save 04 Oct 2017 18:21 #15342

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
Hi

I have a form, where I like to add some additional info like user id to the form submission - this is data that should not be presented to the user as a form field.
In my view.html.php I can echo $model_client->getItems()->id and get the id I would like to add, but not sure how I pass it to the save function in the model.

Any help is appreciated :-)

Please Log in or Create an account to join the conversation.

Inject data to a form save 04 Oct 2017 20:33 #15343

If you want the current userid save to the database, you can add the wizard "Author" to the table.
That will create a field "created_by" and "modified_by" for you :)
When submitting the form, that data will be filled.

In general for injecting data to the form, you have to fork the controller save() function.

Docs about forks: www.j-cook.pro/index.php/f/forks

First add the hidden field (i.e. yourfield) to the table, but not to the form.
When you have forked the controller file, add in that file:
	public function save($key = null, $urlVar = null)
	{
            //check for session
	    JSession::checkToken() or JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));
            // get the form
	    $jinput = JFactory::getApplication()->input;
	    $data  = $jinput->post->get('jform', array(), 'array');
            $user = JFactory->getUser();
            //inject data
	    $data["yourfield"] = $user->id; //can be any data, depending on your needs 
            // set the post var
	    $jinput->post->set('jform',$data);
            // call save of the parent
 	    parent::save($key, $urlVar);
       }

Copy the form xml from models/forms/yourtable.xml to fork/models/forms/yourtable.xml
Remove all fields from the fieldset "yourtable.form" and add
		<field name="yourfield"
				alias="yourfield"
				label=""
				filter="INT"
				type="hidden"/>

By adding an extra field of type hidden, you should see that field as an hidden input on your form (check if it is).
Then, because you are calling parent::save the normal process will be executed (with saving the model to the database with your injected data)
The following user(s) said Thank You: admin, dyvel

Please Log in or Create an account to join the conversation.

Inject data to a form save 04 Oct 2017 22:05 #15345

  • dyvel
  • dyvel's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
  • Posts: 200
  • Thank you received: 11
Thank you!
Your post was very helpfull - got it working :-)

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
Time to create page: 0.081 seconds

Awards for the best Joomla app. This product is gonna win an award for this amazing job. Cook Self Service is the the best application from all over the Joomla universe ! It brings Joomla to a professional level really advanced for developers. It is a real fun to develop with it. The ACL part and security checks implementation are just... so much hours saved. I can now concentrate myself more on the design part and the creative works. Thank you so much. Guys I offer you all my congratulation ! Keep up the works because Joomla is needing it to increase the quality of extensions availables on the JED. I also learned a lot because I can see how to code at the proper place and I found all my answers reading the forum.
lack_hanson (JED)
          

Get Started