3

I am working on a custom magento admin module with grids. When you add a new entry, I perform custom validation and throw an error (when & if it occurs) using Mage::getSingleton('adminhtml/session')->addError() method.

The error message I set does not appear, when I redirect back to the edit form.

This is my save action on the grid controller:

public function saveAction()
{
    // Look For HTTP Post
    if ($data = $this->getRequest()->getPost())
    {
        // Load Data
        $manualOrderSyncModel = Mage::getModel('mycompany_mymodule/manualordersync')
            ->setData($data)
            ->setId($this->getRequest()->getParam('id'));

        // Anticipate Errors
        try
        {
            // Get If Order Number Is Valid
            $order = Mage::getModel('sales/order')->load($manualOrderSyncModel->getOrderNumber(), 'increment_id');
            if (null === $order->getId())
                throw new Exception('No such order exists in the system. Check that order number.');

            // Check If This Order Already Exists In Queue
            $existingManualOrderSyncModel = Mage::getModel('mycompany_mymodule/manualordersync')
                ->load($manualOrderSyncModel->getOrderNumber(), 'order_number');
            if (null !== $existingManualOrderSyncModel->getId())
            {
                // Update Existing Entry
                $existingManualOrderSyncModel
                    ->setCreatedAt(now())
                    ->setCreatedBy(Mage::getSingleton('admin/session')->getUser()->getUsername())
                    ->setIsSynced(Mycompany_Mymodule_Model_Yesno::NO)
                    ->save();
            }
            else
            {
                // Update Timestamps
                if ($manualOrderSyncModel->getCreatedAt() == NULL) {
                    $manualOrderSyncModel
                        ->setCreatedAt(now())
                        ->setCreatedBy(Mage::getSingleton('admin/session')->getUser()->getUsername());
                }
                $manualOrderSyncModel->save();
            }

            // Set Success
            Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Manual order sync updated.'));
            Mage::getSingleton('adminhtml/session')->setManualordersyncData(false);

            // Handle Redirect
            $this->_redirect('*/*/');
            return;
        }
        catch (Exception $e)
        {
            // Error
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            Mage::getSingleton('adminhtml/session')->setManualordersyncData($data);
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            return;
        }
    }

    // Error
    Mage::getSingleton('adminhtml/session')->addError($this->__('Invalid request - unable to find manual order sync to save.'));
    $this->_redirect('*/*/');
}

I have noticed, the issue only occurs when I do this:

// Error
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setManualordersyncData($data);
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;

However, If set error and redirect back to grid like this, the error message shows:

// Error
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setManualordersyncData($data);
$this->_redirect('*/*/');
return;

This is not ideal because I am no longer in the edit form and I have lost the old data from the form. Any ideas on how to fix this?

Latheesan
  • 23,247
  • 32
  • 107
  • 201

2 Answers2

1

Experiencing exactly this issue myself, disappointing that this question hasn't received any replies

Alan A
  • 2,557
  • 6
  • 32
  • 54
0

The reason is that your edit page is not configured to display the session messages. Instead the message persists until the first time it is displayed, and can even bulk up with multiple "copies" of the same message if you keep trying over and over.

The answer is to make sure that your controller and layout is outputting the messages.

The bare minimum required is the following lines in your controller:

$this->loadLayout();
// other code goes here...

$this->_initLayoutMessages('adminhtml/session');  // <--- this line is key
$this->renderLayout();

If you are using a session other than the adminhtml/session, then init those messages instead.

Phil M
  • 432
  • 7
  • 16
  • I know this comment is a bit on the late side, but I think it worth mentioning that while this answer is mostly correct, if you look at `Mage_Adminhtml_Controller_Action` `loadLayout` you will see `$this->_initLayoutMessages('adminhtml/session');` is called, so there should be no need to call it yourself. The "gotcha" for me was I was calling `loadLayout` twice which seems to either recreate the messages block or wipe out its messages. – FearTheBeard88 Nov 04 '21 at 12:22