1

I am new in Joomla component development. I have used this document to develop the component. In this document there is a function loadFormData in Helloworld model as below:

protected function loadFormData()
    {
        // Check the session for previously entered form data.
        $data = JFactory::getApplication()->getUserState(
            'com_helloworld.edit.helloworld.data',
            array()
        );

        if (empty($data))
        {
            $data = $this->getItem();
        }

        return $data;
    }

I wanted to know:

  1. Is this loadFormData method used to load the form fields input when the page reload?
  2. How I can call and use this method in other part of my component?
  3. Does this method keeps the input data even if the form is not submitted?
  4. Where does JFactory::getApplication()->setUserState is done?

Any help is appreciated.

1

I ran into this as well and have learned the following: the loadFormData can indeed used to load previously saved data back into the form and Joomla does not seem to set the form data to the user state automatically. You have to do that somewhere.. with -indeed- setUserState.

The comment "// Check the session for previously entered form data." set me thinking and on a search for the how and why.

In my case I have 2 fields, from_date and to_date. In my com_myform\model\forms\myform.xml they are set to <field name="from_date" type="calendar" [..] default="NOW" /> and <field name="to_date" type="calendar" [..] default="NOW +1 week" /> so they are set to today and the same day next week.

In my form I wanted to keep the initial dates in case the (frontend) user entered some other value wrong. It should give an error message and fill the same form with the (previously) entered dates so the user could change other values without having to start over again.

In my helper function (com_myform\helper\processmyform.php) I do an availability check but before I continue I "save" the form data as below:

  public function checkAvailability($fromDate, $toDate)
    {
   JFactory::getApplication()->setUserState( 'com_bookit.edit.bookit.data', 
array('to_date' => $toDate, 'from_date' => $fromDate) );
 ... }

As you can see I saved them in an array. Now in the loadFormData() I can easily access them! So in my com_myform\models\myform.php I do:

protected function loadFormData()
{
    // Check the session for previously entered form data
        $data = JFactory::getApplication()->getUserState(
            'com_bookit.edit.bookit.data',
            array('to_date', 'from_date')
        );
        return $data;
    }

And magically the data is inserted into the form. In this way you can populate form data with previously saved (and even changed) values.

Please note that you have to empty the UserState if you don't want to use the values over and over. After the (frontend) user dates were accepted and the form had to be set to the default dates, I would do a JFactory::getApplication()->setUserState( 'com_bookit.edit.bookit.data', null);

I hope it helps someone!

0

I think this method is for loading data in the form .The method first checking the session for get data if the session array be empty it get data from database So The data can be in session or in database. This method don't have any responsibility to load the form but it just for loading data in form and get the data that should be injected in the form. And for getting form you should use the getForm method that is a abstract method for getting the form from the model.

0

1. Is this loadFormData method used to load the form fields input when the page reload?

loadFormData() populates the data array with the data stored in the session ( getUserState() ) or if empty from the database ( $this->getItem() ).

https://docs.joomla.org/How_to_use_user_state_variables

2. How I can call and use this method in other part of my component?

Just load the model:

JLoader::import('joomla.application.component.model');
JLoader::import( 'user', JPATH_SITE . DS . 'components' . DS . 'com_helloworld' . DS . 'models' );
$your_model = JModelLegacy::getInstance('your_model','HelloWorldModel');
$formdata= $your_model->yourFunction();

Component in module. addModelPath doesn't seem to work

The loadFormData is a protected function, make it public or write a public function which calls your loadFormData() within the model.

http://php.net/manual/en/language.oop5.visibility.php

3. Does this method keeps the input data even if the form is not submitted?

No. This would only work if you submit the form via Ajax to the session ( setUserState() ) while the user is still editing.

https://docs.joomla.org/Using_Joomla_Ajax_Interface

4. Where does JFactory::getApplication()->setUserState is done?

In the save function of the model controller for example:

https://github.com/joomla/joomla-cms/search?p=1&q=setUserState+&type=&utf8=%E2%9C%93

I don't know if Joomla sets the form data to the user state automatically while loading / saving the form.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.