Displaying error messages

In this recipe, we will see how we can display messages, such as error messages when a user input is found to be incorrect. We will create a view that will have a button that displays a message when the input is correct (for simplicity's sake, we will focus on the error generation coding in this recipe).

In addition, it is recommended that you use assistance class for storing language-dependent text that is to be displayed in the form of messages. For simplicity's sake, the assistance class has not been shown in this recipe.

How to do it...

  1. In the first step, we will create a Button element on our view and give it the caption Check Data. Within the events, we will select BUTTON (as the onAction property) from the listbox.
    How to do it...
  2. A public attribute MYMESSMANAGER is defined at the component controller level, the associated type of which is the IF_WD_MESSAGE_MANAGER interface.
    How to do it...
  3. Next, we write the relevant code within the WDDOINIT method of the component controller. We define an interface reference for the IF_WD_CONTOLLER interface.
  4. The WD_GET_API method is then used to get a reference to the interface containing appropriate APIs. We will then call the GET_MESSAGE_MANAGER method in order to get the handle to the message manager. This is assigned to the WD_THIS->MYMESSMANAGER interface we defined in the previous section.
    How to do it...
  5. In the WDDOBEFOREREACTION method of the appropriate view, we will write the coding for generating an error message. First, a reference to the IF_WD_VIEW_CONTROLLER interface is defined. The WD_GET_API method is then called to get the relevant set of APIs for the view controller. This is then returned to the LO_API_CONTROLLER variable.
  6. We use the GET_CURRENT_ACTION method in order to find out which of the events have been triggered by the user. The name of the event triggered is returned in the LO_ACTION->NAME attribute. We check if the name is equal to the BUTTON element, that is, the button we define. We then carry out the necessary checks, and if the data entered is invalid, the REPORT_ERROR_MESSAGE method of the IF_WD_MESSAGE_MANAGER interface is called with the appropriate error text. In this case, the public attribute, MYMESSMANAGER, defined in the component controller, is used.
    How to do it...

How it works...

In this case, the interface used for generating messages is the IF_WD_MESSAGE_MANAGER interface. The interface has a number of methods used for generating warning error messages on the screen.

First, we defined a public attribute MYMESSAGEMANAGER at the component controller level. Appropriate code is written in order to get the handle to the message manager that can be later used from any of the views in order to generate messages.

When the user clicks the button we defined, the WDDOBEFOREREACTION method of the relevant view is called. Within the method, we wrote code to gain access to the API of the view controller, the reference to which is used to call the GET_CURRENT_ACTION method. The GET_CURRENT_ACTION method is used for determining the event that has been raised as a result of the user interaction. We checked the LO_ACTION->NAME attribute in order to see whether our button has been clicked. In case our button is clicked, we carry out the necessary checks for checking the validity of the data that is entered (the code is not shown). Then the REPORT_ERROR_MESSAGE method is called in order to display an error message.

How it works...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.151.71