Jump to content

Prestashop 1.6 helper form add select option?


Recommended Posts

Hi,

I just installed prestashop 1.6 on my localhost and tried to make a module. So in module I just used a form . So for craeting form I tried this code




  public function renderForm()
  {
    $fields_form = array(
      'form' => array(
        'legend' => array(
          'title' => $this->l('My ModuleSettings'),
          'icon' => 'icon-cogs'
        ),
        'input' => array(
          array(
            'type' => 'text',
            'label' => $this->l('Site Url'),
            'name' => 'YOUR_NAME',
            'desc' => $this->l('Enter Your Name'),
            'lang' => true,
          ),
          array(
            'type' => 'select',
            'lang' => true,
            'label' => $this->l('Link Target'),
            'name' => 'WEBSITE_LINK_TARGET',
            'desc' => $this->l('Please Eneter Web Site URL Address.'),
            'options' => array(
              'query' => $options,
              'id' => 'id_option', 
              'name' => 'name'
            ),
            $options = array(
              array(
                'id_option' => 1, 
                'name' => 'Method 1' 
              ),
              array(
                'id_option' => 2,
                'name' => 'Method 2'
              ),
          )
          ),
        'submit' => array(
          'title' => $this->l('Save')
        )
      ),
    );
    
    $helper = new HelperForm();
    $helper->show_toolbar = true;
$helper->table =  $this->table;
    $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
    $helper->default_form_language = $lang->id;
    $helper->module = $this;
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitmymoduleconf';
    $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'uri' => $this->getPathUri(),
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
    return $helper->generateForm(array($fields_form));
  }


But this code is not showing the select options input fields in the form. Whenever I am trying to load the page it is showing error like  Undefined variable: options. I have gone through the documentation from this link http://doc.prestashop.com/display/PS16/Using+the+HelperForm+class but still can't able to solve this issue. So if someone kindly help me to solve this I will be really appreciable. Thanks.

Link to comment
Share on other sites

 

Hi,
I just installed prestashop 1.6 on my localhost and tried to make a module. So in module I just used a form . So for craeting form I tried this code

  public function renderForm()
  {
    $fields_form = array(
      'form' => array(
        'legend' => array(
          'title' => $this->l('My ModuleSettings'),
          'icon' => 'icon-cogs'
        ),
        'input' => array(
          array(
            'type' => 'text',
            'label' => $this->l('Site Url'),
            'name' => 'YOUR_NAME',
            'desc' => $this->l('Enter Your Name'),
            'lang' => true,
          ),
          array(
            'type' => 'select',
            'lang' => true,
            'label' => $this->l('Link Target'),
            'name' => 'WEBSITE_LINK_TARGET',
            'desc' => $this->l('Please Eneter Web Site URL Address.'),
            'options' => array(
              'query' => $options,
              'id' => 'id_option', 
              'name' => 'name'
            ),
            $options = array(
              array(
                'id_option' => 1, 
                'name' => 'Method 1' 
              ),
              array(
                'id_option' => 2,
                'name' => 'Method 2'
              ),
          )
          ),
        'submit' => array(
          'title' => $this->l('Save')
        )
      ),
    );
    
    $helper = new HelperForm();
    $helper->show_toolbar = true;
$helper->table =  $this->table;
    $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
    $helper->default_form_language = $lang->id;
    $helper->module = $this;
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitmymoduleconf';
    $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'uri' => $this->getPathUri(),
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
    return $helper->generateForm(array($fields_form));
  }
But this code is not showing the select options input fields in the form. Whenever I am trying to load the page it is showing error like  Undefined variable: options. I have gone through the documentation from this link http://doc.prestashop.com/display/PS16/Using+the+HelperForm+class but still can't able to solve this issue. So if someone kindly help me to solve this I will be really appreciable. Thanks.

 

Hello,

Any solution out there?

Link to comment
Share on other sites

  • 4 months later...

You should try the following :

public function renderForm()
  {
    $options = array(
              array(
                'id_option' => 1, 
                'name' => 'Method 1' 
              ),
              array(
                'id_option' => 2,
                'name' => 'Method 2'
              ),
    );
    $fields_form = array(
      'form' => array(
        'legend' => array(
          'title' => $this->l('My ModuleSettings'),
          'icon' => 'icon-cogs'
        ),
        'input' => array(
          array(
            'type' => 'text',
            'label' => $this->l('Site Url'),
            'name' => 'YOUR_NAME',
            'desc' => $this->l('Enter Your Name'),
            'lang' => true,
          ),
          array(
            'type' => 'select',
            'lang' => true,
            'label' => $this->l('Link Target'),
            'name' => 'WEBSITE_LINK_TARGET',
            'desc' => $this->l('Please Eneter Web Site URL Address.'),
            'options' => array(
              'query' => $options,
              'id' => 'id_option', 
              'name' => 'name'
            )
            
          ),
        'submit' => array(
          'title' => $this->l('Save')
        )
      ),
    );
  • Like 1
Link to comment
Share on other sites

  • 6 months later...

I got that problem earlier too  and got the solution

 

try this code

array(
                'type' => 'select',                              
                'label' => $this->l('Category:'),
                'name' => 'category',                    
                'required' => true,                             
                'options' => array(
                            'query' => $options = array(
                                        array(
                                          'id_option' => 1,       // The value of the 'value' attribute of the <option> tag.
                                          'name' => 'Method 1'    // The value of the text content of the  <option> tag.
                                        ),
                                        array(
                                          'id_option' => 2,
                                          'name' => 'Method 2'
                                        ),
                            ),
                            'id' => 'id_option',
                            'name' => 'name' 
                           )
            )
Edited by duazo_joesel (see edit history)
  • Like 3
Link to comment
Share on other sites

×
×
  • Create New...