2

I have created a custom grid in the admin panel using UI components. Everything is working fine except that there is no success/error message shown on the grid page.

If I redirect the controller after saving action to the edit record page, it shows the success/error message.

I have also tried to redirect to some other grid after saving the data, and the message is being shown correctly there.

I have matched almost everything of my grid with another working grid, but could not find any clue yet.

Can anyone help me out to find the problem?

Please let me know if you need to see the code.

Update: Added code below.

Grid Controller (Which has problem):

<?php
namespace Stack\RuleBasedDiscount\Controller\Adminhtml\Rules;

class Index extends \Magento\Backend\App\Action
{
    const ADMIN_RESOURCE = 'Stack_RuleBasedDiscount::rules';
    protected $resultPageFactory;

    public function __construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $this->_view->loadLayout();
        $this->_setActiveMenu('Stream_RuleBasedDiscount::rules');
        $this->_addBreadcrumb(__('Rule Based Discount'), __('Rule Based Discount'));
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('Rule Based Discount'));
        return $resultPage;
    }
}

Save Controller:

<?php

namespace Stack\RuleBasedDiscount\Controller\Adminhtml\Rules;

class Saveedit extends \Magento\Backend\App\Action {

    protected $_ruleModel;
    protected $_storeManager;

    public function __construct(
    \Magento\Backend\App\Action\Context $context, \Stack\RuleBasedDiscount\Model\Rule $ruleModel, \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_ruleModel = $ruleModel;
        $this->_storeManager = $storeManager;
        parent::__construct($context);
    }

    public function execute() {
        try {
            $data = $this->getRequest()->getParams();
            $this->_ruleModel->setData($data);
            $this->_ruleModel->save();
            $message = __('Rule information has been saved successfully.');
            $this->messageManager->addSuccess($message);
            $this->_redirect('rulebaseddiscount/rules/index');
            return;
        } catch (\Exception $e) {
            $message = __('There was some problem in saving rule information. Please try again later.');
            $this->messageManager->addError($message);
            $this->_redirect($this->_redirect->getRefererUrl());
            return;
        }
    }
}
7
  • Can you please add your code so we can get easily some idea? Commented Oct 10, 2018 at 12:02
  • code For UI grid? Commented Oct 10, 2018 at 12:05
  • No . Controller file in which action are you not able to see messages? Commented Oct 10, 2018 at 12:06
  • can you please share your save controller code?
    – Abdul
    Commented Oct 10, 2018 at 12:06
  • @MohitKumarArora Please share your saving action code
    – Abdul
    Commented Oct 10, 2018 at 12:14

2 Answers 2

0

You can write :

please try below and check once

$message="There was some problem in saving rule information. Please try again later";

$this->messageManager->addError(_($message));

or

$this->messageManager->addErrorMessage($message);

1
0

Try below code:

public function execute()
{
    try {
        $data = $this->getRequest()->getParams();
        $this->_ruleModel->setData($data);
        $this->_ruleModel->save();
        $this->messageManager->addSuccess(__('Rule information has been saved successfully.'));
        $this->_redirect('*/*/');
        return;
    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        $this->messageManager->addError($e->getMessage());
        $this->_redirect($this->_redirect->getRefererUrl());
        return;
    } catch (\Exception $e) {
        $message = __('There was some problem in saving rule information. Please try again later.');
        $this->messageManager->addError($message);
        $this->_redirect($this->_redirect->getRefererUrl());
        return;
    }
    $this->_redirect('*/*/');
    return;
}

OR

public function execute()
{
    if ($data = $this->getRequest()->getPostValue()) {
        try {
            $ruleModel = $this->_objectManager->create('Stack\RuleBasedDiscount\Model\Rule');
            $ruleModel->setData($data);
            $ruleModel->save();
            $this->messageManager->addSuccess(__('Rule information has been saved successfully.'));
            $this->_redirect('*/*/');
            return;
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
            $this->_redirect('*/*/postAction');
            return;
        } catch (\Exception $e) {
            $message = __('There was some problem in saving rule information. Please try again later.');
            $this->messageManager->addError($message);
            $this->_redirect('*/*/postAction');
            return;
        }
    }
    $this->_redirect('*/*/');
    return;
}

Note: Please put your referer url above code for eg. "$this->_redirect('adminhtml/abc/postAction');"

2
  • What is the problem in my code? Commented Oct 10, 2018 at 12:43
  • @MohitKumarArora I think your have not added proper redirect action in your code
    – Abdul
    Commented Oct 10, 2018 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.