How to show custom notifications or error message at admin panel for prestashop 1.7.7.7?

1.5k Views Asked by At

​ Hi, 

I've been trying a lot of options to manage an exception and show an error at admin panel but nothing seems to work.

I'm at the postProcess method of a custom module. After the user sends a csv file through a form and the data is checked (everything works fine here), if an exception occurs I need to show a message, stop and redirect to the same page. 

I've tried this: 

this->get('session')->getFlashBag()->add('error',$msg);
Tools::redirectAdmin('index.php?controller='.$controller.'&token='.$token);

this: 

header("HTTP/1.0 400 Bad Request");
die(json_encode(array( 'error' => array($this->l(' Error') ))));

(that one works but shows a blank page with the message, not the message inside the admin panel) 

also this: 

$this->context->smarty->assign(array(
    'token' => Tools::getAdminTokenLite('AdminModules'),
    'errors' => $this->errors
));
$this->setTemplate('ExcelProcess.tpl');

and {$errors|var_dump} at the tpl displays null...

... and many other options. 

I can't find anything either about backoffice custom notifications at the PS docs, only about front custom notifications.

Any clue? 

 

Thanks a lot! 

Miguel

PostProces code: https://drive.google.com/file/d/175nhUPDlzi6T8rZjjE8Desnzq-mtYzNQ/view?usp=sharing

​Tpl code: https://drive.google.com/file/d/17EONOCJ60L4Gp_GidzvwCQwMyTrRXapF/view?usp=sharing

1

There are 1 best solutions below

2
On

Adding an error in postProcess() can be achieved by setting

$this->errors[] = $this->l('My error');

or

$this->context->controller->errors[] = $this->l('My error');

during your form submission checks. Form will be rendered with your error messages into a red box.

If you want to show an alert without reloading the page instead, you'll have to perform an AJAX call to your AdminController, get back a JSON response and render your error message as a result of the execution of the call.

See offical docs