0

I have created a custom admin grid and an associated form in magento 2. While saving a item the success message is not displayed. Do I need any special component in grid to add success message in my save controller Or do I need to inject message manager interface. If this shouldn't be happening please provide me with a solution, thanks Save.php

<?php



use X\Y\Model\BannerFactory;

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

    protected $_model;
    protected $_resultRedirectFactory;
    protected $_authSession;
    public function __construct(
    \Magento\Backend\App\Action\Context $context, BannerFactory $model, \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory, \Magento\Backend\Model\Auth\Session $authSession
    )
    {
        $this->_model = $model;
        $this->_resultRedirectFactory = $resultRedirectFactory;
        $this->_authSession = $authSession;
        parent::__construct($context);
    }

    public function execute()
    {
        $id = $this->getRequest()->getParam('id');
        try {
            $modelData = $this->_model->create();
            //If gets id loads the model
            if ($id) {
                $modelData = $modelData->load($id);
            }

            $formData = $this->getRequest()->getParam('general');
            $imageData = $this->getRequest()->getParam('upload');

            $user = $this->getCurrentUser();

            if (empty($formData ['id'])) {

                $formData['id'] = null;
                // update the current user name for both created and updated
                $formData['created_by'] = $user;
                $formData['updated_by'] = $user;
            }

            $formData['updated_by'] = $user;
            //For small image
            if (isset($imageData['small_image'])) {
                $formData['small_image'] = $imageData['small_image'][0]['name'];
            }

            //Set all the data in DB from form
            $modelData->setData($formData);
            $modelData->save();
            $this->messageManager->addSuccess(__("Your Slide has been saved !"));
        } catch (Exception $e) {
            $this->messageManager->addError($e->getMessage());
        }
        //Result redirected to index controller     
        $resultRedirect = $this->_resultRedirectFactory->create()->setPath('*/*/index');
        return $resultRedirect;
    }

    public function getCurrentUser()
    { //Get the current user name using session
        return $this->_authSession->getUser()->getUsername();
    }

}
| improve this question | | | | |
1

If your controller extends \Magento\Backend\App\Action I believe this should work, before doing the redirect

$this->messageManager->addSuccess(__('Your success message here'));

Or...

$this->getMessageManager()->addSuccess(__('Your success message here'));

Update: It could be a layout problem then. More info https://magento.stackexchange.com/a/127689/3566

| improve this answer | | | | |
  • I am adding messge in above manner only. I need to show this message in grid page. – Jaisa Feb 8 '18 at 4:35
  • Answer updated, it seems layout problem – Raul Sanchez Feb 8 '18 at 7:49
  • @RaulSanchez how to show success message in Custom Model page ? – Aditya Shah May 30 '18 at 9:39
  • I think you'd add a new question for that @Aditya – Raul Sanchez May 30 '18 at 11:09
  • Yes sure i am adding a question for that and sending you the url :) if you are aware about then please answer – Aditya Shah May 30 '18 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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