简体   繁体   中英

Joomla 3.x, how to keep user data if validation fail

i have a problem when coding my new Joomla component. I have some input form in my custom joomla component (/components\\com_custom\\views\\myview\\tmpl\\default.php )

<input type="text" class="form-control" name="jform[checkindate]" id="checkindate" value="<?php if (isset($post)) {echo $post; } else { echo 'Checkin date'; }?>">

Of courses, at the top i have this:

$jin = JFactory::getApplication()->input;
$post = $jin->get('checkindate', 'Checkin date', 'STR');

My point is to keep user post data if they faile in validation so they don't need to retype all the form, they only need to fix some error data before they can submit it again, but there is no success. Could you please help me on this? Thank you so much!

I'm assuming you are using Joomla jform for building forms

In jform, certain things need to take care if you want to maintain the form session.

  1. Store the submitted form data in session - Before redirecting the user to form view after validation. you need to store the user data in user state. This is generally done in the controller file. This done by following way

    $app = JFactory::getApplication(); $app->setUserState('componentname.formname.data', $data);

Please note that the first parameter is the key for your data which is stored in the user state. the second parameter is the Jinput data post by the user.

  1. Load form data from the session if available - Till now we have saved the data in the session now we need to check whether form data available in session or not. This Is done in the load form data method written in the model.

`

protected function loadFormData()

{
    $data = JFactory::getApplication()->getUserState('componentname.formname.data', array());

    if (empty($data))
    {
        // This is the my component function to load form data
        $data = $this->getData();
    }

    return $data;
}

3 . If the form data saved successfully then don't forgot to clear it from the session.

$app->setUserState('componentname.formname.data', null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM