Reputation: 69
Here is my Java code, before call I call the save()
method. I want to check this business rule.
if (endDate.before(startDate)){
message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "",
"The end date should be not before the start date.");
// Throw exception so that it prevents document from being saved
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.addMessage("travel_endDate",message);
return false;
}
I want this message display at the "travel_endDate" component.
Upvotes: 1
Views: 325
Reputation: 10485
You have to use the client id of your component when adding the message.
1.) Add a binding to you component
<xp:inputText
id="travel_endDate"
binding="#{errorComponent}">
</xp:inputText>
2.) resolve the variable in your save method
UIComponent cmp = (UIComponent) facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, "errorComponent");
String clientId = cmp.getClientId(facesContext);
3.) Add the message with the id to the facesContext
facesContext.addMessage(clientId,message);
Upvotes: 1