Validation feedback in widget

2
In a custom widget an onchange microflow is called. In this microflow a validation message is created. This results in an call of the error handler where the messages can be extracted from the passed object and displayed on screen. So far so good. But still a popup message displayed. How can I prevent this?
asked
1 answers
8

Validation messages should be handled with subscriptions instead of extracting the validations from the raw response. You can subscribe to validation updates by using this.subscribe:

this.subscribe({
    val : true,
    guid : guid,
    callback : function(val) {
        // handle validations
    }
});

Now when there are validation messages for the object you are subscribed to, the callback function will be called (before the error handler is called). The callback function is called with one argument: a validation object (for legacy reasons this object is wrapped in an array, but this array will always contain only one item).

The validation object contains all validation messages for the object you subscribed to. To let the client know that your widget handles the validation message for a particular attribute, the validation message must be removed from the validation object. Only validation messages not handled by anything else will be shown in a popup.

Example (guid is the guid of the Mendix object, attr is the name of the attribute):

this.subscribe({
    val : true,
    guid : guid,
    callback : function(validations) {
        var validation = validations[0],
            message = validation.getErrorReason(attr);

        if (message != null) { // if there is a validation message for the attribute we're interested in
            // let the client know we handle this validation message
            validation.removeField(attr);

            // show the validation to the user
            // ...
        }
    }
});
answered