How can I determine if an error occured during server side validation on an XPage?

When a field fails validation I can use a “Display Error” control to display an error message for a certain field (control) on an XPage.I would like to compute the visibility of an element depending on whether a certain field has failed validation. Basically I am looking for the information which is used to determine whether the xp:message control is rendered or not to be exposed in the context or requestScope or wherever appropriate.

Is this information available somewhere?

Subject: use UIInput.isValid and FacesContext.getMessages

The information is available using the JSF APIs.

The component corresponding to an edit box is a javax.faces.UIInput

(same applies to a combo box or any other field that inputs a field value).

During the validation, if the field is not valid, UIInput.isValid will be set to false.

Also during validation, FacesMessage objects are added to the facesContext

for each control that fails validation. If the control has multiple validators that fail,

then multiple messages will be added.

For example:

Paste this source into an XPage and Preview. If you click the submit button without entering a value

in the edit box, then the computed fields will display the error messages.

<?xml version="1.0" encoding="UTF-8"?>

<xp:view xmlns:xp=“http://www.ibm.com/xsp/core”>

A required field:<xp:br></xp:br>

<xp:inputText id="inputText1" value="#{requestScope.something}"

	required="true" disableClientSideValidation="true">

	<xp:this.validators>

		<xp:validateRequired message="The field is required."></xp:validateRequired>

	</xp:this.validators>

</xp:inputText>

<xp:br></xp:br>



<xp:button value="Submit" id="button1">

	<xp:eventHandler event="onclick" submit="true"

		refreshMode="complete" immediate="false" save="true"></xp:eventHandler>

</xp:button>

<xp:br></xp:br>

<xp:br></xp:br>



The messages associated with inputText1:

<xp:br></xp:br>

<xp:text escape="false" id="computedField1">

	<xp:this.value><![CDATA[#{javascript:

var input:javax.faces.component.UIInput = getComponent(‘inputText1’);

if( input.isValid() ){

// no message

return "";

}

var clientId = input.getClientId(facesContext);

// the messages for that input

var messagesIter:Iterator = facesContext.getMessages(clientId);

var result = “”;

while( messagesIter.hasNext() ){

var messageObj:javax.faces.application.FacesMessage 

	= messagesIter.next();

result += messageObj.getSummary()+ "<br/>";

}

return result;}]]></xp:this.value>

</xp:text>

<xp:br></xp:br>

<xp:br></xp:br>

All messages:

<xp:br></xp:br><xp:text escape="false" id="computedField2">

	<xp:this.value><![CDATA[#{javascript:

// all messages

var messagesIter:Iterator = facesContext.getMessages();

var result = “”;

while( messagesIter.hasNext() ){

var messageObj:javax.faces.application.FacesMessage 

	= messagesIter.next();

result += messageObj.getSummary() + "<br/>";

}

return result;}]]></xp:this.value>

</xp:text>

</xp:view>

Hope that helps.

Subject: Thank You!

Huge help! Thanks for posting this.

Subject: That’s what I was looking for

Thank you, Máire, that’s exactly what I was looking for. Problem solved :slight_smile: