Krzysztof Tomaszewski
Krzysztof Tomaszewski

Reputation: 1144

JSF custom message for validation error

I'm using GlassFish 3.1.2.2 and I want to define a generic validation message template to not include field identifier/label.

I've found couple of related topics on StackOverflow but none of these solutions (all involving javax.faces.validator.BeanValidator.MESSAGE) is working.

1) placing ValidationMessages.properties in root of classpath (in my case it was placed in WEB-INF/classes of my WAR file) - no effect

2) defining the ValidationMessages.properties file in <message-bundle> in faces-config.xml - no effect

3) defining the ValidationMessages.properties file in <resource-bundle> in faces-config.xml - no effect

I tested that resource bundle is correctly working as I can use it in EL, example: #{text['javax.faces.validator.BeanValidator.MESSAGE']} where text is my var from resource-bundle definition.

NOTE: I don't want to give validatorMessage attribute on each of input fields in my application. I just want to setup my custom message once for whole application.

IMPORTANT: the solution presented here Mkyong.com is not working as well.

NOTE: I'm declaring to use JSF 2.0 and Glassfish 3.1.2.2 certainly supports that. NOTE: I don't want to implement validation in managed bean instead of using JSF validation/Bean Validation.

Upvotes: 0

Views: 2683

Answers (2)

Krzysztof Tomaszewski
Krzysztof Tomaszewski

Reputation: 1144

I'm answering myself: to set custom message for required-field validation you need to set following properties in messages bundle file (message bundle is defined in faces-config.xml by <message-bundle> tag):

1) javax.faces.component.UIInput.REQUIRED_detail - a message that is by default presented in h:message or h:messages tags - or when showDetail=true (it is by default). GENERALLY IT'S ENOUGH.

2) javax.faces.component.UIInput.REQUIRED - a message that is presented when showSummary=true in h:message and h:messages tags. Set this additionally if you display summary in your h:message/h:messages.

NOTE: this is not true that javax.faces.validator.BeanValidator.MESSAGE is responsible for required-field-error messages.

Upvotes: 2

psi
psi

Reputation: 269

Add <h:messages> in your jsp/xhtml and to display error message from Managed Bean depending upon condition Use

FacesContext ctx = FacesContext.getCurrentInstance();
FacesMessage msg =new FacesMessage(FacesMessage.SEVERITY_INFO, errorMessage,detailMessage);
ctx.addMessage(null, msg);

Upvotes: 0

Related Questions