UI validation message handling: Message manager

After implementing user input validation for SAPUI5 input fields we can handle them using standard events. But if you observe the validation message, it will be ‘Invalid value‘ for all the the fields regardless of input type. This standard message text will not help much for user to fill the fields correctly. So we need to customize the message text using message manager or any other methods.

What you will learn,

  • Enabling standard message validation feature.
  • Adding custom message instead of automatic message.
  • Handling message model data using message manager.
Standard Error Message

1. Enabling automatic message creation feature


Automatic Error Message

There is a feature in UI5 which provides more useful validation error messages for user. This creates automatic messages based on the regular expression or the type we use for input value path. By default this property value will be set to false. We can enable this feature by any of the below ways. Following any one of the below methods will serve our purpose.

Enable from Component container options

Add handleValidation: true property to component container in index file as shown below.

Enable from manifest file

In manifest, with ‘SAP.UI5’ section we can add handleValidation: true as shown below.

Enabling for a view

This will enable automatic message creating for particular view.

Enabling for a control

This will enable automatic message creation only for the specified control.

2. Adding custom message instead of automatic message


Custom value state message

We need to use Message manager for handling all UI messages. But there is a quick way to do it using a method setValueStateText. We have already seen how we can handle validation error and success using event handlers. In those event handler function, we can set value state text to modify the error message.

Simple form in view

Function handler for validation error

3. Handling message model data using message manager


Message manager

Handling messages using message manager is another way of customizing. Using message manager, we can handle both UI and Server message. All messages will be stored and managed in message model. There are several options available with message manager. You can explore more at API reference.