0

I want to add custom error message when specific it satisfies specific condition. Below is the code I wrote on controller. The error message is getting displayed when i navigate to some other page,however this is not showing error message in same page.

Ideally it should show error message on the same page and don't want to show error anywhere else.

public function testAction(){

      $this->loadLayout()->_initLayoutMessages('customer/session'); 

      if(!isset($_FILES['docname']['name']) && $_FILES['docname']['name'] == ''){
        Mage::getSingleton('customer/session')->addError('Custom error message');
      }

      $this->renderLayout();
}
4
  • have you add a massage block in your layout? Commented Nov 11, 2014 at 12:12
  • yes, 1column.phtml as template which is by default calling message block Commented Nov 11, 2014 at 12:15
  • when you have a message on other page, that's mean that the message block doesn't call in priviouse. So, check if you realy call a massege block. Commented Nov 11, 2014 at 12:18
  • yes it is calling message block, when i refresh the page, error message appears. using magento 1.9. and my parent theme is rwd/default Commented Nov 11, 2014 at 12:56

1 Answer 1

1

If you force the page to reload, the error message will be displayed on same page:

if (!isset($_FILES['docname']['name']) && $_FILES['docname']['name'] == '') {
    $this->_getSession()->addError('Custom error message');
    $this->_redirect('*/*/');
    return;
}

Another way to display an error message on the same page is by overwriting the _prepareLayout() function in your block:

protected function _prepareLayout() {
    // IF statement
        $this->getMessagesBlock()->addError('Custom error message');
    // End of IF
    return parent::_prepareLayout();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.