Jump to content

Validation before saving product


Jibu

Recommended Posts

I am using PS 1.7
Created new module for saving additional dimensions for a products.

https://prnt.sc/jgxgeh

public function hookDisplayAdminProductsExtra($params){              
   $id_product =0;
   if (isset($params['id_product']) && (int)$params['id_product']) {
       $id_product =(int)$params['id_product'];
    }
   
    $sql = 'SELECT * FROM '._DB_PREFIX_.'_dimensions WHERE id_product = '.$id_product;
    $dimensions = Db::getInstance()->ExecuteS($sql);

    $this->context->smarty->assign(array(
        'id_product' => $id_product,
        'dimensions' => $dimensions,
    ));
    return $this->display(__FILE__, 'views/templates/admin/form.tpl');
}
//Saving Data
public function hookActionProductUpdate($params){        
        $id_product =(int)$params['id_product'];
        $_asj_pkg_dim_width=Tools::getValue('asj_pkg_dim_width');
        $_asj_pkg_dim_height=Tools::getValue('asj_pkg_dim_height');
        $_asj_pkg_dim_depth=Tools::getValue('asj_pkg_dim_depth');
        $_asj_pkg_dim_weight=Tools::getValue('asj_pkg_dim_weight');                
        
        //Deleting old items
        Db::getInstance()->delete('products_package_dimensions', 'id_product ='.$id_product);
        
        //insert 
        Db::getInstance()->insert('products_package_dimensions', array(
            'id_product' => $id_product ,
            'width' =>$_asj_pkg_dim_width,
            'height'=>$_asj_pkg_dim_height,
            'depth'=>$_asj_pkg_dim_depth,
            'weight'=>$_asj_pkg_dim_weight,
        ));                
        
}

Its working fine.


How to validate this form?
Need to validate numbers only and required field
 

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

  • 3 years later...
On 5/12/2018 at 4:34 PM, ndiaga said:

Hi,

 

The validation is handled by  the function : hookActionProductUpdate($params)   use the Submit and stay button in the template.

this happens after saving product. How to validate before saving?

Link to comment
Share on other sites

1 hour ago, ndiaga said:

Hi,

The   validation  can  be   done  inside  the   function  with  an  if   condition.

That   part  is  PHP   not  related  to  PrestaShop  or   a  Hook.

So how to do it? Update the product again? I want to raise an error on some conditions when admin updates a product with some special features.

Link to comment
Share on other sites

24 minutes ago, ndiaga said:

How   would  you  do  it  in  a  normal  PHP   code?

This  is  PHP    compentance   not  PrestaShop  familiarity .

public   function  verifyCondition($conditions){

$message='';

if($conditions=='Some  Data'){     
//   do  somehing
}
else{
    $message='Your  Alert message';
}

return  $message;

}

 

I found it working like this:

 

public function hookActionObjectProductUpdateBefore($params){
  $flag = false;

  //some calculation inverts this flag to true
  $flag = true;

  if($flag === true){
    throw new PrestaShopException('Custom error goes here');
    die;
  }
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...