Forums › Forums › OroCRM › OroCRM – Programming Questions › Create Custom Widget Error Message
This topic contains 7 replies, has 2 voices, and was last updated by zhex900 6 years, 8 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- August 9, 2017 at 7:39 pm #25466
Hi,
How do I create a custom widget error message?
I want to change this error message to something more meaningful.This is what I have now. But error message is only displayed after I refresh the page.
PHP123$msg = 'Please select a tag!';$this->container->get('session')->getFlashBag()->add('error', $msg);throw new \Exception($msg); - CreatorTopic
- AuthorReplies
- August 17, 2017 at 3:53 am #25467
Hello
By default, it is no possible to customize error message when server returns 500 code. Please wrap you code by try-catch and pass error message to your widget template.
August 17, 2017 at 4:14 am #25468Example
controller:PHP12345678910public function updateAction(){try {throw new \Exception('My Exception Message');// action implementaion} catch (\Exception $e) {return ['exception' => $e->getMessage()];}}twig:
12345678910{# Resources/.../views/widget/update.html.twig #}<div class="widget-content"data-page-component-module="orofrontend/js/app/components/widget-form-component">{% if exception is defined %}<div class="widget-content"><div class="alert alert-error">{{ exception }}</div></div>{% endif %}{# form fields #}</div>August 17, 2017 at 5:13 am #25469@zhex900, did you get an answer to your question?
August 17, 2017 at 5:32 am #25470I want to throw the exception other places not just in the controller. For example EventListeners, Managers. Where do I add the twig you suggested? Basically, I want to have a way to display error messages.
August 17, 2017 at 2:07 pm #25471For wrap exceptions for dialogs globally, you need create and register exception listener, like that:
PHP1234my_bundle.event_listener.exception_listener:class: MyBundle\EventListener\ExceptionListenertags:- { name: kernel.event_listener, event: kernel.exception }PHP123456789101112131415161718192021class ExceptionListener{/*** @param GetResponseForExceptionEvent $event*/public function onKernelException(GetResponseForExceptionEvent $event){if ($event->getRequest()->isXmlHttpRequest()) {$e = $event->getException();$event->setResponse(new JsonResponse(['exception' => ['message' => $e->getMessage()]],Response::HTTP_INTERNAL_SERVER_ERROR));}}}create you widget (extended from “oro/dialog-widget”), overwrite current logic, and register it instead of “oro/dialog-widget” on you bundle (see method _onContentLoadFail), like that:
YAML1234567#Resources/configs/requirejs.ymlconfig:paths:# backup original widget'orowindows/js/widget/dialog-widget': 'orowindows/js/widget/dialog-widget'# overwrite original widget'oro/dialog-widget': 'mybundle/js/widget/dialog-widget'JavaScript1234567891011121314151617181920212223242526//Resources/public/js/widget/dialog-widget.jsdefine(function(require) {'use strict';var DialogWidget;var BaseWidget = require('orowindows/js/widget/dialog-widget');DialogWidget = BaseWidget.extend({/*** @inheritDoc*/_onContentLoadFail: function(jqxhr) {// you own logic for show error messagesvar message = jqxhr.responseJSON.exception.message;var failContent = '<div class="widget-content">' +'<div class="alert alert-error">' + message + '</div>' +'</div>';this._onContentLoad(failContent);}});return DialogWidget;});August 22, 2017 at 3:26 pm #25472@zhex900, this time did you get an answer to your question?
August 22, 2017 at 5:57 pm #25473Yes. Thank you!
- AuthorReplies
The forum ‘OroCRM – Programming Questions’ is closed to new topics and replies.