Wednesday, July 01, 2015

PrestaShop Admin Area Hook Error Reporting

I needed to validate data, report any errors found, and stop the submission of the form while developing a module for the PrestaShop 1.6(.0.14) admin area. However, the documentation was a bit thin on how to do this, and I needed quite a bit of digging before I found what I needed.

I can only tell you 100% for sure that this works for the actionProductUpdate hook, but I imagine it's valid everywhere. Here's a quick snippet to show you the general idea....
public function hookActionProductUpdate($params)
  if (!Validate::isInt (Tools::getValue('pwr_start')))
    $this->context->controller->errors[] = "Ya screwed up!";

There are 3 main things you want to take note of:
  • Use the Validate class to validate your input data
  • Use Tools::getValue instead of directly pulling things from $_GET / $_POST. This does some light purification of the input data.
  • If you want to stop the submit of the form, add the error to the controller's error list. Unfortunately, this is only reported at the top of the form.