3

I try to reload a form with filled data using getUserState. I have read this document many times but I haven't understand where in my view o model I should set the userStatae . I know I should getuserState in my model with some thing like this.

$mainframe =JFactory::getApplication();
$stateVar = $mainframe->getUserState( "option.state_variable", 'state1' );

Can some one help me about where to setuserState and how I pass getUserState value to my layout?

3

This is the way I do it, I hope it helps you.

in you view.html.php (view file)

 $app   = JFactory::getApplication();    
$this->data = $app->getUserStateFromRequest('com_yourcomponent.yourform.form.data','data');

    if (!this->data)
    {
      //Set default values
      $this->data['field1'] = '';
      $this->data['field2'] = '';
      $this->data['field3'] = 1;
    }

In you template file.

The form fields have to be like this:

<input type = "text"  name  = "data[field1]"
     value      = "<?php echo $this->data['field1'] ?>" >                                  

In your function in your controller get the data this way

  $app  = JFactory::getApplication();    
$data   = $app->getUserStateFromRequest('com_yourcomponent.yourform.form.data','data');

If there is an error you redirect to the same form page and the values would be there and if you commit the changes you have to flush the info before redirect using this:

$app->setUserState('com_yourcomponent.yourform.form.data', null);

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.