Magento – adminhtml/session addError not showing after redirect

adminhtmlerrorgridsession

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.

Best Answer

Here is an example of how I use admin sessions on my forms. I use a protected function to get the adminhtml session

/**
 * Retrieve adminhtml session model object
 *
 * @return Mage_Adminhtml_Model_Session
 */
protected function _getSession()
{
    return Mage::getSingleton('adminhtml/session');
}

Then, when I need it, for error messages I call it this way, from inside a controller

    $order          = $this->_getOrder();
    if(!(int)$order->getId())
    {
        $this->_getSession()->addError('Unable to load test order');
        $this->_redirect('*/*/index');
        return false;
    }

That should produce a message like this on the redirect enter image description here

Here is my process on creating forms in Magento for the admin. this example is to send emails from our site to test them rather than having to do the action like place an order, or add a comment to a shipment for example.

Step 1, create the module declaration in app/etc/modules This should be named something like Russell_Emaillog.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Russell_Emaillog>
            <active>true</active>
            <codePool>local</codePool>                
        </Russell_Emaillog>
    </modules>
</config>

Step 2: Create the Namespace folder to contain our code in app/code/local

Russell so it should look like app/code/local/Russell

Step 3: create all the folders needed for this module in app/code/local/Russell

Emaillog Then inside Emaillog create several folders here is the structure

app/code/local/Russell/Emaillog/Block
app/code/local/Russell/Emaillog/Block/Adminhtml
app/code/local/Russell/Emaillog/Block/Adminhtml/Abstract
app/code/local/Russell/Emaillog/Block/Adminhtml/Entry
app/code/local/Russell/Emaillog/Block/Adminhtml/Entry/Edit
app/code/local/Russell/Emaillog/Block/Adminhtml/Entry/Edit/Tab
app/code/local/Russell/Emaillog/Controller
app/code/local/Russell/Emaillog/Controller/Entry
app/code/local/Russell/Emaillog/controllers
app/code/local/Russell/Emaillog/controllers/Adminhtml
app/code/local/Russell/Emaillog/controllers/Adminhtml/Emaillog
app/code/local/Russell/Emaillog/etc
app/code/local/Russell/Emaillog/Helper
app/code/local/Russell/Emaillog/Model
app/code/local/Russell/Emaillog/Model/Email
app/code/local/Russell/Emaillog/Model/Resource
app/code/local/Russell/Emaillog/Model/Entry
app/code/local/Russell/Emaillog/sql
*** These next two are in app/design/adminhtml/default/default/ ***
app/design/adminhtml/default/default/template/russell/
app/design/adminhtml/default/default/layout/russell/

Step 4: Now that you have all the folders in place you need to put the PHP files in each, I will just put the names of the files for this step so you can create empty php and xml files for now

app/code/local/Russell/Emaillog/Block/Adminhtml/Abstract/Entry/Edit/Tab/General.php
app/code/local/Russell/Emaillog/Block/Adminhtml/Abstract/Entry/Edit/Form.php
app/code/local/Russell/Emaillog/Block/Adminhtml/Abstract/Entry/Edit/Tabs.php
app/code/local/Russell/Emaillog/Block/Adminhtml/Abstract/Entry/Edit.php
app/code/local/Russell/Emaillog/Block/Adminhtml/Abstract/Entry/Grid.php
app/code/local/Russell/Emaillog/Block/Adminhtml/Abstract/Entry.php
app/code/local/Russell/Emaillog/Block/Emailtemplate.php
app/code/local/Russell/Emaillog/Controller/Entry/Abstract.php
app/code/local/Russell/Emaillog/controllers/Adminhtml/Emaillog/EntryController.php
app/code/local/Russell/Emaillog/controllers/Adminhtml/Emaillog/TestemailController.php
app/code/local/Russell/Emaillog/etc/adminhtml.xml
app/code/local/Russell/Emaillog/etc/config.xml
app/code/local/Russell/Emaillog/etc/system.xml
app/code/local/Russell/Emaillog/Helper/Data.php
app/code/local/Russell/Emaillog/Model/Email/Template.php
app/code/local/Russell/Emaillog/Model/Resource/Entry/Collection.php
app/code/local/Russell/Emaillog/Model/Resource/Entry.php
app/code/local/Russell/Emaillog/Model/Entry.php
app/code/local/Russell/Emaillog/sql/russell_emaillog_setup/install-0.0.1.php
app/design/adminhtml/default/default/template/russell/emaillog/view.phtml
app/design/adminhtml/default/default/layout/russell/emaillog.xml

My response was too large to have this all in one Answer, this is part 1

Related Topic