Thursday, October 7, 2010

XPages: Add global/field message

If you want to set a message in a xp:message/xp:messages control, here's a little code snippet for you.

// Sets a global message/message for a field
function addFacesMessage( message, component ){
try {
if( typeof component === 'string' ){
component = getComponent( component );

var clientId = null;
if( component ){
clientId = component.getClientId( facesContext );

facesContext.addMessage( clientId,
new javax.faces.application.FacesMessage( message ) );
} catch(e){ /*Debug.logException(e);*/ }

If the second parameter isn't specified, the message becomes global (visible in all rendered xp:messages controls on the page).

If you specify the second parameter, it has to be the component id of a field which has a xp:message control, or the field component. The message then shows up in the message control for the field.

Code stolen/ported from this page.

Share and enjoy!


Anonymous said...


Thank you for your code. I implemented it in the field checking script library. It works properly on the browser, but on the server console appears the following JVM warning:
HTTP JVM: WARNING: XPages server validation errors haven't been displayed to the user
After this message the text list of the error messages displayed on the console as well.
Do you have any idea what can cause this?
Thank you in advance

Tommy Valand said...

Is it a partial refresh? Is the message/-s control refreshed, so that the message is shown?

Currently I'm using OpenLog (from OpenNTF) for debug messages, so I haven't used this code in a while.

Anonymous said...

Yes, it is a partial refresh.
Messages dispayed within the controls properly in the browser
The same messages appear on the server console after the HTTP JVM warning. E.G.: User forget enter value in a mandatory txt field. When clicking on the Save button, the control appears with the "Field is mandatory txt". At the same time on the Domino server console:
HTTP JVM: WARNING: XPages server validation errors haven't been displayed to the user
HTTP JVM: WARNING: [0]"Field is mandatory txt"
Hopefully more clear this way

Tommy Valand said...

Not sure what it can be. There's a bug report on that relates to this message. Not sure if it's due to the same issue.

Mervin said...

XPages server validation errors haven't been displayed to the user ==> If you are using try catch remove it, I was having a similar problem. You will know where the error is. for me, it was not getting the handle of the document in data source (computed for document id).