如何在 prestashop 1.7.7.7 的管理面板上显示自定义通知或错误消息?

发布于 2025-01-20 22:27:56 字数 1175 浏览 4 评论 0 原文

嗨, 

我一直在尝试很多选择来管理异常,并在管理面板上显示错误,但似乎没有工作。

我正在使用自定义模块的后进程方法。用户通过表单发送CSV文件并检查数据后(这里一切正常),如果发生异常,我需要显示一条消息,停止并重定向到同一页面。 

我已经尝试过: 

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

这个: 

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

(一个人可以使用,但显示了一个带有消息的空白页,而不是管理面板内的消息) 

也是这样: 

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

和{$ errors | var_dump}在tpl显示null ...

和许多其他选项。 

我找不到关于PS文档的退回式自定义通知的任何内容,只有关于前面的自定义通知。

有任何线索吗? 

 

非常感谢! 

MIGUEL

邮政编码代码:

Tpl code:


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如此安好 2025-01-27 22:27:56

添加 postprocess()中的错误

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

可以通过设置或

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

在表单提交检查中 。
您的错误消息将渲染到红色框中。

如果要显示警报而不重新加载页面,则必须对AdminController执行Ajax调用,恢复JSON响应并呈现错误消息,导致呼叫的执行。

参见

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文