0

I want to display the error notice when a specific rule match when we edit the customer informations on backend. I tried with default \Exception class or Magento specific but it show the error as text page instead of the red notice usually used. Ideally, I wish to display the red notice with a custom message (Address street has to be less or equal to 35 characters).

1 exception(s):
Exception #0 (Exception): Address street has to be less or equal to 35 characters.

Exception #0 (Exception): Address street has to be less or equal to 35 characters.
<pre>#1 Magento\Customer\Controller\Adminhtml\Index\Save\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#2 Magento\Customer\Controller\Adminhtml\Index\Save\Interceptor->___callPlugins() called at [generated/code/Magento/Customer/Controller/Adminhtml/Index/Save/Interceptor.php:26]
#3 Magento\Customer\Controller\Adminhtml\Index\Save\Interceptor->execute() called at [vendor/magento/framework/App/Action/Action.php:107]
#4 Magento\Framework\App\Action\Action->dispatch() called at [vendor/magento/module-backend/App/AbstractAction.php:235]
#5 Magento\Backend\App\AbstractAction->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#6 Magento\Customer\Controller\Adminhtml\Index\Save\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#7 Magento\Customer\Controller\Adminhtml\Index\Save\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/module-backend/App/Action/Plugin/Authentication.php:143]
#8 Magento\Backend\App\Action\Plugin\Authentication->aroundDispatch() called at [vendor/magento/framework/Interception/Interceptor.php:135]
#9 Magento\Customer\Controller\Adminhtml\Index\Save\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#10 Magento\Customer\Controller\Adminhtml\Index\Save\Interceptor->___callPlugins() called at [generated/code/Magento/Customer/Controller/Adminhtml/Index/Save/Interceptor.php:39]
#11 Magento\Customer\Controller\Adminhtml\Index\Save\Interceptor->dispatch() called at [vendor/magento/framework/App/FrontController.php:55]
#12 Magento\Framework\App\FrontController->dispatch() called at [vendor/magento/framework/Interception/Interceptor.php:58]
#13 Magento\Framework\App\FrontController\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
#14 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
#15 Magento\Framework\App\FrontController\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:26]
#16 Magento\Framework\App\FrontController\Interceptor->dispatch() called at [vendor/magento/framework/App/Http.php:136]
#17 Magento\Framework\App\Http->launch() called at [vendor/magento/framework/App/Bootstrap.php:257]
#18 Magento\Framework\App\Bootstrap->run() called at [pub/index.php:37]
</pre>

Thanks in advance!

ps: Late merry Christmas and early happy new year!

1 Answer 1

0

I finally found how to do it with the following code:

$this->_messageManager->addExceptionMessage(new \Exception(__('Address street has to be less or equal to 35 characters.')), __('Address street has to be less or equal to 35 characters.'));
return $this->resultRedirectFactory->create()->setPath($this->redirect->getRedirectUrl());

You will need to add Magento\Framework\App\Response\RedirectInterface and Magento\Framework\App\Action\Context in the __construct method of the controller.

public function __construct(
    ManagerInterface $messageManager,   
    Context $context,
    RedirectInterface $redirect
)
{
    $this->_messageManager = $messageManager;
    $this->resultRedirectFactory = $context->getResultRedirectFactory();
    $this->redirect = $redirect;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.