Jump to content

using post variable in prestashop not doing validation in validator


Recommended Posts

Hi,

I am doing a small module in prestashop 1.6. In the module I have the code something like this

  


public function getContent() 
  {
    $site_url = Tools::getValue('FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT'));
    
    if (Tools::isSubmit('submit'))
    {
      if (empty($_POST['FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT')]))
        $this->_html .= $this->displayError($this->l('You must fill the field.'));
      else
      {
        if(Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'mymodule`(`module_id`,`site_url`) VALUES ("","'.$site_url.'")')) 
        {
          $this->_html .= $this->displayConfirmation($this->l('Settings updated.'));
        }
        else 
        {
          $this->_html .= $this->displayError($this->l('You have some errors.'));
        }
      }
    }
  }

  

  This one is doing insert the values to the database. But when I am doing validate the module in prestashop validator it is showing error like this

 


  The use of $_POST is forbidden; use Tools::getValue() instead

  

  But when I tried to solve the error by its suggestion with  this code 

  

    


public function getContent() 
  {
    $site_url = Tools::getValue('FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT'));
    
    if (Tools::isSubmit('submit'))
    {
      if (empty(Tools::getValue('FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT'))))
        $this->_html .= $this->displayError($this->l('You must fill the field.'));
      else
      {
        if(Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'mymodule`(`module_id`,`site_url`) VALUES ("","'.$site_url.'")')) 
        {
          $this->_html .= $this->displayConfirmation($this->l('Settings updated.'));
        }
        else 
        {
          $this->_html .= $this->displayError($this->l('You have some errors.'));
        }
      }
    }
  }

  it is showing error like


Fatal error: Can't use function return value in write context

  So can someone kindly tell me how to solve this issue? Any help and suggestions will be really appreciable. Thanks

Edited by prestashop_newuser (see edit history)
Link to comment
Share on other sites

ok i see now :)

if (empty($_POST['FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT')]))

so, you used:

if (empty(Tools::getValue('FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT'))))

Tool::getValue function returns:

- if variable exists: value of variable (from post, get etc.)

- if variable doesnt exist: it returns false(bool)

 

so it can't be "empty" :)

 

if i vere you i will use if condition like:

if (Tools::getValue('FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT')) !=false)
Link to comment
Share on other sites

 

ok i see now :)

if (empty($_POST['FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT')]))

so, you used:

if (empty(Tools::getValue('FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT'))))

Tool::getValue function returns:

- if variable exists: value of variable (from post, get etc.)

- if variable doesnt exist: it returns false(bool)

 

so it can't be "empty" :)

 

if i vere you i will use if condition like:

if (Tools::getValue('FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT')) !=false)

@Vekia thanks for the answer. I just changed your code like this

if (Tools::getValue('FACEBOOKACTIVITY_URL_'.Configuration::get('PS_LANG_DEFAULT')) !=true)

and it worked. Thanks

Link to comment
Share on other sites

×
×
  • Create New...