Jump to content

[SOLVED]Form validation for module


 Share

Recommended Posts

Hello ! 

 

I created a module with the help of this tutorial :  http://nemops.com/prestashop-products-new-tabs-fields/#.VW6y4s_tlBd

 

Know I would like to add a form verification  for the "custom_field", for exemple just int or just text. I tried with vallidate.php who is in already in prestashop but it didn't work:

 

I put my verification in "public function hookActionProductUpdate($params)" just after the foreach so that if the validation isn't ok, the database didn't update ! and i put this for the verification :

if(!Validate::isInt($custom_field)
{
DiplayError.....
}
else
{
Update the database
}

Firt I don't know if my verification realy has to go in this class and then when i submit, the database never update even if my entre is an INT... and my display Error spawn no error.. I think I have the wrong syntaxe !

I tried with preg_match but didn't work too : if (preg_match('/^[0-9]+$/m', $custom_field)
But work in .tpl file with smaty : {if preg_match('/^[0-9]+$/m', $custom_field[1])} 
But I can't stop update in .tpl file...

(I work with PS version 1.6.0.14)

Thank you in advance ! =)

Edited by jeanjeanjeana (see edit history)

Share this post


Link to post
Share on other sites

Yes i removed it but it was like this : (if(!Validate::isInt($code_douanier) = from prestashop Validate.php)

 

public function hookActionProductUpdate($params)
{
// get all languages
// for each of them, store the custom field!
$id_product = (int)Tools::getValue('id_product');
$languages = Language::getLanguages(true);
 
if(!Validate::isInt($code_douanier)
{
die(Tools::displayError('Code douanier in not an INT'));
}
else
{
foreach ($languages as $lang) {
if(!Db::getInstance()->update('product_lang', array('pays_origine'=> pSQL(Tools::getValue('pays_origine_'.$lang['id_lang']))) ,'id_lang = ' . $lang['id_lang'] .' AND id_product = ' .$id_product ))
$this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
if(!Db::getInstance()->update('product_lang', array('code_douanier'=> pSQL(Tools::getValue('code_douanier_'.$lang['id_lang']))) ,'id_lang = ' . $lang['id_lang'] .' AND id_product = ' .$id_product ))
$this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
}
}
$this->UpdateOderDetail();
 
}
 
But this doesn't work si I tried this : 
 
public function hookActionProductUpdate($params)
{
// get all languages
// for each of them, store the custom field!
$id_product = (int)Tools::getValue('id_product');
$languages = Language::getLanguages(true);
 
if(!preg_match('/^[0-9]+$/m', $code_douanier)
{
die(Tools::displayError('Code douanier in not an INT'));
}
else
{
foreach ($languages as $lang) {
if(!Db::getInstance()->update('product_lang', array('pays_origine'=> pSQL(Tools::getValue('pays_origine_'.$lang['id_lang']))) ,'id_lang = ' . $lang['id_lang'] .' AND id_product = ' .$id_product ))
$this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
if(!Db::getInstance()->update('product_lang', array('code_douanier'=> pSQL(Tools::getValue('code_douanier_'.$lang['id_lang']))) ,'id_lang = ' . $lang['id_lang'] .' AND id_product = ' .$id_product ))
$this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
}
}
$this->UpdateOderDetail();
 
}
Doesn't work too ....
Edited by jeanjeanjeana (see edit history)

Share this post


Link to post
Share on other sites

Hello !! 

 

I solved my problem ! 

 

I override validate.php and created my own verification function and then I override product.php and add my two variables ( line 200) and then in "public static $definition = array(" att the end i add my two fields  : 

 

'pays_origine' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'My created verification function'),
'code_douanier' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'My other created verification function'),
 
Thanks for you help =) 
Edited by jeanjeanjeana (see edit history)

Share this post


Link to post
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

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