Watch the video to learn about formatters and data types.
In this lesson, we will first discuss data types. The formatters will follow in the next lesson.
Objective
Watch the video to learn about formatters and data types.
In this lesson, we will first discuss data types. The formatters will follow in the next lesson.
As mentioned previously, data types allow you to format model data for display on the UI as well as parse and validate user input.
The currently available data types all inherit from the sap.ui.model.SimpleType class. For a complete list of all these simple types, see the API Reference under the sap.ui.model.type namespace.
In this training course, the following three simple types are briefly introduced in examples: sap.ui.model.type.Integer, sap.ui.model.type.Date and sap.ui.model.type.Currency.
In addition to the simple types supplied by SAP, it is also possible to define your own custom data types. For this purpose, a subclass of SimpleType has to be created. In this subclass, a custom implementation for the methods formatValue, parseValue, and validateValue from the SimpleType class is made.
The following parameters can be passed via the constructor of a simple type:
Simple types can be specified in data binding to define the data type of the model data. The complex binding syntax is used in XML views for this purpose.
If an entered value cannot be parsed successfully or is not within the defined constraints, an exception is raised. This results in the entered value not being transferred to the model.
If you press Enter or move the focus to a different UI control, SAPUI5 executes the parse and validation function belonging to the corresponding data type.
If parse or validation exceptions occur due to incorrect user input, these errors are not automatically displayed on the UI.
You can use the message manager for reporting these error messages back to the user. To do it, you must register the corresponding UI elements with the message manager. There are several ways to do this.
You can activate automatic message generation via the message manager in the sap.ui5 section of the manifest.json application descriptor or as a parameter when instantiating a component. You can also activate it for individual controls by registering the controls in the message manager using the registerObject method.
In the example shown in the figure Validation Handling Using the Message Manager, the handleValidation property in the application descriptor is used to enable validation handling by the message manager for the component.
As a result, parse and validation errors are handled by the message manager for all UI elements used in the component. That is, any parse and validation error messages generated based on the user input are picked up by the message manager, which in turn passes the error message back to the correct UI element for display.
A field in error has a red border. The error message itself is only displayed when the field has focus.
In this exercise, you will add validations and formatting to the application. To do this, you will first enable automatic message creation for input validation using the application descriptor. Then, you will add Simple Types to some of the data bindings you have already created to validate or format data.
Template: | Git Repository: https://github.com/SAP-samples/sapui5-development-learning-journey.git, Branch: sol/13_element_binding |
Model solution: | Git Repository: https://github.com/SAP-samples/sapui5-development-learning-journey.git, Branch: sol/14_data_types |
Log in to track your progress & complete quizzes