Skip to Content
Technical Articles
Author's profile photo Enric Castella Gonzalez

SAPUI5 – User message compilation

When we make user interfaces in SAPUI5 there is a point that it’s necesary to thinking befor coding… What do we show the user to indicate that there are errors or everything is correct?

In this post I try to collect the most used messages in SAPUI5 to give feedback to the user:

Message Strip

Messages integrated in the screen that allow us to show information and errors (it will remind you of the old SAP GUI messages maybe)

To show it we add in the view the following code:

    text="Este formulario es provisional"


Message View

Messages related to an element or component of the view. These messages can have the content we want and open like a bullet

In this case is necesary add some code in view and some code in controller, but this type of message is the most complete

First off all, It’s necesary to add the libraries:

	"sap/ui/model/BindingMode", // <-- Add this library
	"sap/ui/core/message/Message", // <-- Add this library
], function (Controller,JSONModel,BindingMode,Message) {

In the controller we add the following code. First of all is register the object handler and then add the model (this model is an example)

onInit: function () {		
            var oMessageManager, oModel, oView;

            oView = this.getView();
// Register message handler
            oMessageManager = sap.ui.getCore().getMessageManager();
            oView.setModel(oMessageManager.getMessageModel(), "message");            
            oMessageManager.registerObject(oView, true);

// Create example model for input parameters
            oModel = new JSONModel({
                MandatoryInputValue: "",
                DateValue: null,
                IntegerValue: undefined,
                Dummy: ""


When the user add some data incorrectly, a warning appears, to give more detail, we will shoot a fragment by adding this function

_getMessagePopover : function () {
        // create popover lazily (singleton)
        if (!this._oMessagePopover) {
            this._oMessagePopover = sap.ui.xmlfragment(this.getView().getId(),"<namespace_of_aplication>.fragments.MessageError", this);
        return this._oMessagePopover;

In the project, we will create a new folder called “fragment” and then, in this folder, we add a file called “MessageError.fragment.xml” with the following code:


Finally in the view, we add some fields with a standard formetter

		<Label text="Nombre" required="true"/>
		<Input id="Nombre" value="{ path: '/MandatoryInputValue', type: 'sap.ui.model.type.String', constraints: { minLength: 3 } }"/>
		<Label text="Fecha"/>
		<DatePicker id="date"
			value="{ path:'/DateValue', type:'sap.ui.model.type.Date', formatOptions: { style: 'short', strictParsing: true }, constraints: { } }"/>
		<Label text="ValoraciĆ³n (0-10)"/>
		<Input id="int" value="{ path: '/IntegerValue', type:'sap.ui.model.type.Integer', constraints: { minimum: 0, maximum: 10 } }"/>


Message Box

Typical pop ups with one or more action buttons used as confirmation or warning messages when we want full user attention

For these messages we will add code in the controller

     'sap/m/MessageBox' // <--- ADD this library
 ], function (Controller,JSONModel,BindingMode,Message,MessageBox) {

This function call the message:

    lanzarMessageBox: function(oEvent) {
        var bCompact = !!this.getView().$().closest(".sapUiSizeCompact").length;
            "Seguro que quieres cerrar esta ventana?", {
                styleClass: bCompact ? "sapUiSizeCompact" : ""

In this example, we launch the message from a button in a view:

 text="Message Box"


Message Toast

These messages appear and disappear without the user doing anything. Typically used when everything goes well

For these messages we will add code in the controller

	"sap/m/MessageToast" // <-- Add this library
], function (Controller,JSONModel,BindingMode,Message,MessageBox,MessageToast) {
	"use strict";

The following code laungh the message, in this case add the trigger in “onInit” function

onInit: function () {"Hola, has iniciado el controlador :) ");


With these types of messaging you can cover practically all the cases that the user needs



Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.