首页 > 解决方案 > Magento 2成功默认消息在同一个phtml文件中

问题描述

单击提交按钮后,我想打印成功消息(Magento2 默认消息格式)。因此,如果所有字段都已验证,那么在同一 phtml 文件上应该有成功消息“您的数据已验证!”,我想知道如何从基本开始执行此操作,因为我不知道它::

我的块文件的代码::

` <?php
namespace Test\Validation\Block;

class Index extends \Magento\Framework\View\Element\Template{
    protected $helper;
    protected $_messageManager;
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        array $data = []) {
        parent::__construct($context, $data);
        $this->_messageManager = $messageManager;
    }
    public function successmessage($message){
        $this->_messageManager->addSuccess(__($message));
    }

}` 

~~谢谢,提前1!

标签: phpmagentocontrollerblock

解决方案


It should be done in a controller but not in the block. Look the example in the native magento contact module here \Magento\Contact\Controller\Index\Post


推荐阅读