Validation

Binding validation messages

In some cases, it is not practical to use model binding in all fields of a PMO (e.g. because conversion/navigation needs to be done in getters/setters), but it should still be possible to set validation messages on a field.

This can be done with the annotation @BindMessages. Apply it to a field, then create a method with the signature MessageList get<FIELD_NAME>Messages(MessageList). Additional filtering can be applied within this method, e.g. to only display error messages:

    @BindMessages
    @UITextField(position = 30, label = "Interested in all error messages")
    public String getOnlyErrorsTextField() {
        return "This component will show all validation error messages from all the other components";
    }

    public MessageList getOnlyErrorsTextFieldMessages(MessageList messages) {
        return messages.stream()
                .filter(m -> Severity.ERROR == m.getSeverity())
                .collect(MessageList.collector());
    }
The PMO must be registered with a ValidationService.

When working with @BindMessages, please be aware of the following:

  • The messages are processed after all other binding aspects have been processed. That means the component is already in the state it will be in after the update round trip, and any further state changes (e.g. enabling/disabling a field) made in get<FIELD_NAME>Messages will only be applied on the next update. For this reason, we do not recommend changing the component’s state at all in get<FIELD_NAME>Messages.

  • The returned message list should only contain the messages that are really handled by the component in a way that the user could fix the problem using this component.

It is also possible to create a custom message handler.