Jump to content
Venky967

Display same form even after form submission

Recommended Posts

Hello, I have a form with four input text fields and a submit button.So ,after submitting the form the same values I entered in the input text fields must be displayed in the form after page refresh.

Edited by Venky967 (see edit history)

Share this post


Link to post
Share on other sites

A way to go is to override the controller of the page the form is in and set a smarty variable for each field of the form and then put them in your tpl file.

 

Preferably with a check if the smarty variable is set in order to avoid errors in your error log.

Share this post


Link to post
Share on other sites

Inside my main php file , I wrote the form using renderForm function.

Let me elaborate what I have done till now. 
Example: Inside  mymodule.php (my main file)

 

  class mymodule extends Module

   {

 

    /*variables assigned*/

 

     public function __construct()

     {
        
       /* some content*/
 
     }
   
      public function renderForm()
    {
         $helper = new HelperForm();
          
 
           $fields_form = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Page Title'),
                    'icon' => 'icon-cogs'
                ),
                'description' => $form_desc,
            'input' => array(
                    array(
                            'type' => 'select',
                            'label' => $this->l('Select An Option'),
                            'name' => 'optionvalue',
                            'required' => true,
                            'options' => array(
                                'query' => $options,
                                'id' => 'id_desc',
                                'name' => 'name'
                          )
                    ),
                array(
                  'type'     => 'text',
                  'label'    => $this->l('Username'),
                  'name'     => 'username',
                  'size'     => 50,
                  'class' => 'fixed-width-xxl',
                  'required' => true,
                  'desc'     => $this->l('Please enter your username.')
              ),
                array(
                  'type'     => 'password',
                  'label'    => $this->l('PASSWORD'),
                  'name'     => 'password',
                  'size'     => 50,
                  'class' => 'fixed-width-xl',
                  'required' => true,
                  'desc'     => $this->l('Please enter your  password.')
              ),
             'submit' => array(
                  'name' => 'submit',
                  'title' => $this->l('Save'),
                  'icon' => 'process-icon-save',
                  'class' => 'btn btn-default pull-right'
 
              );
 
             return $helper->generateForm(array(
            $fields_form
        ));
      }
 
This is what my main file looks like.So after configuring my module it displays the form with Select option,input text field,password and a Save button.
 
NO issues with database insertion.It's working really nice :)
All I need is after submitting the form,the values I have entered must be displayed inside their respective input fields after form gets submitted.
I have no idea how to use smarty ,where should we add those files
Help me to achieve it.
 
Edited by Venky967 (see edit history)

Share this post


Link to post
Share on other sites

Before generating the form you should assign the values to the fields. So just before 

return $helper->generateForm(array(
            $fields_form));

add 

$helper->tpl_vars = array(
'fields_value' => $this->getConfigFieldsValues(),
);

and declare 

public function getConfigFieldsValues()
    {
        $fields_values = array(
            'optionvalue' => VALUE...
            ),
            'username' => VALUE...
            ),
            ... etc
        );
        return $fields_values;
    }

hope this helps!

  • Like 1

Share this post


Link to post
Share on other sites

That's great Balzo :) 

Everything is working fine except the password value.I'm unable to show its value in input text field.
Help me in getting the password value.

Thanks.

Share this post


Link to post
Share on other sites

public function renderForm()

{

$helper->tpl_vars = array(

fields_value' => $this->getConfigFieldsValues()

);

$fields_form = array(

'input' => array(

array(

'type' => 'password',

'label' => $this->l('PASSWORD'),

'name' => 'password',

'size' => 50,

'class' => 'fixed-width-xl',

'required' => true,

'desc' => $this->l('Please enter your password.')

)

);

return $helper->generateForm(array(

$fields_form

));

}

 

public function getConfigFieldsValues()

{

$password = Tools::getValue('password'); /*Getting the input field value */

$fields_values = array(

'password' => $password

);

 

return $fields_values;

}

 

I am getting the value of $password variable but it is not getting displayed inside the input field.In other case,if I change the input type from password to text then it is dispalyed

So I think there is a problem with type password while displaying.

 

Thank you.

Edited by Venky967 (see edit history)

Share this post


Link to post
Share on other sites

as far as I can tell you only set the input field value only when you submit the form.

 

 I think the code should be like this

public function getConfigFieldsValues()
{

$password = Configuration::get('MYMODULE_ACCOUNT_PASSWORD', null);//or directly from db depending on what logic you implemented in your module

$fields_values = array(
'password' => $password
);

return $fields_values;
}

Share this post


Link to post
Share on other sites

It's from the Configuration class of Prestashop where by using get and set functions you can store variables without the need of you own table in the Db, you can see the class file to understand what it does. Or you can just save the password in your module's table. It is your choice

 

The point I want to make is that you have to pull the data for the input field from your DB and not from the submitted value.

Share this post


Link to post
Share on other sites

My password is in encryption form inside the database.So even after retrieving from the database the value is not displayed.

Share this post


Link to post
Share on other sites

if you want it to be shown maybe consider not saving it as a password. Save it as plain text. On the helper change 'type' => 'password' to 'type'= 'text'

Share this post


Link to post
Share on other sites

Hello, I also have a problem displaying the informations of a saved custom Object in my fields, working with prestashop 1.7.6.
I have a custom module, and inside an admin controller which contains this function to display my custom form :

public function renderForm()
    {
        if (!($obj = $this->loadObject(true))) {
            return false;
        }

        $this->initToolbar();

        $image = $this->image_dir . $obj->id . '.' . $this->image_type;
        $image_url = ImageManager::thumbnail($image, $this->table . '_' . (int) $obj->id . '.' . $this->imageType, 264, $this->imageType, true, true);
        $image_size = file_exists($image) ? filesize($image) / 1000 : false;

        $this->fields_form = [
            'legend' => [
                'title' => $this->module->l('Modifier cet article'),
                'icon' => 'icon-cog'
            ],
            'input' => [
                [
                    'type' => 'text',
                    'label' => $this->module->l('Titre'),
                    'name' => 'title',
                    'lang' => true,
                    'required' => true,
                    'empty_message' => $this->module->l('Le titre'),
                ],
                [
                    'type' => 'text',
                    'label' => $this->module->l('Url'),
                    'name' => 'rewrite',
                    'lang' => true,
                    'required' => true,
                    'empty_message' => $this->module->l('L\'url de l\'article'),
                ],
                [
                    'type' => 'select',
                    'label' => $this->module->l('Type'),
                    'name' => 'type',
                    'lang' => true,
                        'options' => [
                            'query' => [
                                ['key' => '1', 'name' => $this->module->l('Actualité')],
                                ['key' => '2', 'name' => $this->module->l('Bon plan')],
                            ],
                        'id' => 'key',
                        'name' => 'name'
                    ],
                ],
                [
                    'type' => 'textarea',
                    'label' => $this->module->l('Résumé'),
                    'name' => 'short_description',
                    'lang' => true,
                    'autoload_rte' => true,
                    'required' => true,
                ],
                [
                    'type' => 'textarea',
                    'label' => $this->module->l('Contenu'),
                    'name' => 'description',
                    'lang' => true,
                    'required' => true,
                    'autoload_rte' => true,
                ],
                [
                    'type' => 'file',
                    'label' => $this->module->l('Image'),
                    'name' => 'cover',
                    'display_image' => true,
                    'image' => $image_url ? $image_url : false,
                    'size' => $image_size,
                    'delete_url' => self::$currentIndex . '&' . $this->identifier . '=' . $obj->id . '&token=' . $this->token . '&deleteImage=1',
                    'desc' => $this->module->l('Image pour la page toutes les actualités.'),
                    'hint' => $this->module->l('Uploader l’image'),
                    'required' => true,
                ],
                [
                    'label' => $this->module->l('Ajouter une date (optionnel)'),
                    'type' => 'date',
                    'name' => 'date_add',
                ],
            ],
            'submit' => [
                'title' => $this->module->l('Sauvegarder'),
            ],
        ];

        $this->context->smarty->assign(
            array('currentToken' => $this->token)
        );

        return parent::renderForm();
    }


So far everything is working well, It displays the good fields, I can save them and it goes well in my database. But when i want to modify an already created object, It recover my informations except for two fields : The rewrite field which is empty and the select field which is always set to the first option value. 

I thought this part was handled by the renderForm prestashop function, how can I fix this ? Thanks a lot !

 

Capture d’écran 2019-07-25 à 11.58.34.png

Share this post


Link to post
Share on other sites

The rewrite one was because I forgot the field in my ObjectModel 🤦‍♂️

Any ideas for the select field ? 😕 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More