Krish
Krish

Reputation: 357

Magento adding custom error message

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();
}

Upvotes: 0

Views: 1382

Answers (1)

Daniel Kratohvil
Daniel Kratohvil

Reputation: 1261

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();
}

Upvotes: 1

Related Questions