I made a custom module for observer on customer save before event for admin-side

My config file in local/Pavans/Profile/etc




It calls my observer before customer data saved

class Pavans_Profile_Model_Observer {
    public function customerSaveAfter(Varien_Event_Observer $observer) 
        $controller = $observer->getControllerAction();
        $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
        $response = array('error' => false, 'message' => $helper->__('Error'));
        return $controller->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));    


I want to pass error message on customer form and stop controller to save customer data, but above code shows me error message

 Call to a member function setFlag() on a non-object

So i'm unable to get object of controller.

I have also tried this

$request = Mage::app()->getRequest();
$action = $request->getActionName();
Mage::app()->getFrontController()->getAction()->setFlag($action, Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
$response = Mage::app()->getResponse();

$json = Mage::helper('core')->jsonEncode(array('error' => -1, 'message' => $this->__('test errror'))); 

This shows me Error message on form

An error occurred while saving the customer

Above message is not my custom error message

  • why you used this ->setFlag()
    – Amit Bera
    Sep 1 '14 at 12:01
  • 'FLAG_NO_DISPATCH' Stop executing Controller Action So No customer data saved and we can show our error message Sep 1 '14 at 12:02
  • when you got error?? frontend ya backend
    – Amit Bera
    Sep 1 '14 at 12:47
  • setflag not working on both side, and that your code below in your answer shows 'Cannot save the customer' error not my custom error on front-side And 'An error occurred while saving the customer.' in back-end. currently i only want for back-end. I'm using magento Sep 1 '14 at 12:52

Instead of using

Mage::app()->getFrontController()->getAction()->setFlag($action, Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true)



try this for

public function customerSaveAfter(Varien_Event_Observer $observer) 
    try {
         Mage::app()->getResponse()->setBody(Mage::helper('core')->jsonEncode(array('error' => -1, 'message' => $this->__('test errror'))));
    catch (Mage_Core_Exception $e) {
    catch (Exception $e){
        Mage::app()->getResponse()->setBody(Mage::helper('customer')->__('Customer move error'.$e));


See at https://stackoverflow.com/questions/5290639/in-an-observer-method-how-do-you-tell-magento-to-not-process-code-after-the-dis

edit: Can try 200 instead of 500

Or Try this

public function customerSaveAfter(Varien_Event_Observer $observer) 

            $request = Mage::app()->getRequest();


            if( Mage::app()->getRequest()->isXmlHttpRequest() || Mage::app()->getRequest()->getParam('isAjax')) {
                // Is ajax
                $action = $request->getActionName();
                        $action, Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);

                Mage::app()->getResponse()->setHeader('Content-type', 'application/json');

                Mage::app()->getResponse()->setBody(Mage::helper('core')->jsonEncode(array('error' => -1, 'message' => $this->__('test error'))));

            } else {
               // Is form submit
                    ->addError(Mage::helper('customer')->__('test error.'));
               Mage::app()->getFrontController()->getAction()->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
  • I have tried this one. This shows me Error message on form 'An error occurred while saving the customer' Not my custom error message.I don't know why its not working. Sep 1 '14 at 12:14
  • My code for admin-side goes to Is form submit section so i changed 'customer/session' to 'core/session'. But its showing error but also saving all data , so customer saved successfully message. I added Mage::app()->getResponse()->setBody(Mage::helper('core')->jsonEncode(array('error' => -1, 'message' => $this->__('test error')))); . Now its showing me both custom error and 'An error occurred while saving the customer.' as before. this also stopped customer data saving. So i'm using it. Thanks :) Sep 1 '14 at 13:42
  • can you send me exception.log and system.log.thus i check where the issue
    – Amit Bera
    Sep 1 '14 at 13:55
  • Only one system log :- '2014-09-01T14:01:04+00:00 ERR (3): User Error: Some transactions have not been committed or rolled back in C:\wamp\www\magentoanifit\lib\Varien\Db\Adapter\Pdo\Mysql.php on line 3941' Sep 1 '14 at 14:03
  • using this event you want to prevent data save?
    – Amit Bera
    Sep 1 '14 at 14:25

Just throw an Exception:

Mage::throwException('My Error message')

Than the save is avoided and the error message is shown

  • Thanks alot. But this error is not showing on form but on listing on admin-side, though its showing error on form on front-side. Sep 1 '14 at 11:53
  • Is this an AJAX call, so no magento core code? Sep 1 '14 at 11:58
  • Its on adnin-side so its an ajax call, With above code page redirect to listing of all customers and shows our error message. Sep 1 '14 at 12:00
  • ah, got it. In which context are you? You can only check the code for anything which helps you with your task. Creating a new customer? Creating a new customer to create an order? Sep 1 '14 at 12:46
  • 1
    You should checkout \Mage_Eav_Model_Form::validateData , especially where this $dataModel comes from and who to utilize it for what you want to do. Sep 1 '14 at 12:56

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.