Skip to content

Latest commit

 

History

History
132 lines (88 loc) · 4.88 KB

Validation_Messages_a90d93d.md

File metadata and controls

132 lines (88 loc) · 4.88 KB
loio
a90d93df5a024e8bb18826b699c9aaa7

Validation Messages

Validation messages are either created by the framework and processed by thesap.ui.core.message.ControlMessageProcessor or manually by the application.


The target of a validation message can be empty. In this case, the message has no specific target and is relevant for the whole application. If a target is set, the target is a string consisting of a control ID, a slash ("/"), and the name of the property to which the message applies.

Example: label0/text


Validation messages are added with a target referencing a control and its specific property. The messages are kept until a validation message for the property is created and assigned. If new data for the same property is received from the server, the validation messages are erased unless their persistent property is set to true.


Validation messages are generated by the framework type validation when data changes. If a bound property has an assigned type, the validation can trigger the message creation. To activate the automatic message creation, the following options exist:

  • Component:

    You can activate the automatic message generation in the component metadata or as a parameter when instantiating the component as follows:

    // "UIComponent" required from "sap/ui/core/UIComponent"
    // "ComponentContainer" required from "sap/ui/core/ComponentContainer"
    
    UIComponent.extend("MyComponent", {
        metadata  : {
            version  : "1.0" ,
            handleValidation  : true
        }
    });
    var oComponentContainer = new ComponentContainer("MyComponentContainer", {
        name: "MyComponent",
        id: "myComponentId",
        handleValidation: true
    });
  • Descriptor for Applications

    You can activate the automatic message generation in the "sap.ui5" section of the manifest.json file as follows:

    "sap.ui5": {
           "handleValidation": true
    }
  • Control

    You can activate automatic message generation for controls by registering the control in the message manager as follows:

    // "Input" required from "sap/m/Input"
    // "TypeFloat" required from "sap/ui/model/type/Float"
    // "Messaging" required from "sap/ui/core/Messaging"
    
    var oInput = new Input({
        value: { path: "/Products(1)/Price", type: new TypeFloat() }
        value: { path: "/Products(1)/Price", type: new sap.ui.model.type.Float() }
    });
    Messaging.registerObject(oInput, true);

    Note:

    If you don't set the second attribute of Messaging.registerObject to true, any validation / parse error event for the registered object is canceled without a message being generated. For more information, see Messaging.registerObject in the API reference.


Manually Created Messages

You can also create validation messages manually and add them to the sap/ui/core/Messaging module. If you add the message to a control property that is bound and validated by a data binding type, your message gets deleted when new validation results from the type comes in. You can override this behavior by setting the persistent property of the message to true.

// "ControlMessageProcessor" required from module "sap/ui/core/message/ControlMessageProcessor"
// "Messaging" required from module "sap/ui/core/Messaging"
// "Input" required from module "sap/m/Input"
// "FloatType" required from module "sap/ui/model/type/Float"
// "Message" required from modle "sap/ui/core/message/Message"
var oMessageProcessor = new ControlMessageProcessor();

Messaging.registerMessageProcessor(oMessageProcessor);

var oInput = new Input({
    id: "myInputId",
    value: { path: "/Products(1)/Price" , type: new FloatType() }
});

Messaging.addMessages(
    new Message({
        message: "ZIP codes must have at least 23 digits",
        type: sap.ui.core.MessageType.Error,
        target: "myInputId/value",
        processor: oMessageProcessor
     })
);

Related Information

API Reference: sap.ui.core.ControlMessageProcessor

API Reference: module:sap/ui/core/Messaging