0
votes

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.

2
You are using jsf 1.X, aren't you?jmrodrigg
Mkyong solution works for me out of the box (with the zip provided) on glassfish 3.1.2.2gpilotino
@gpilotino I just downloaded Mkyong sample again. When you don't enter any data and click "Submit" then following error messages appear: 1) Username: Validation Error: Value is required. 2) Date of Birth: Validation Error: Value is required. I want these messages to be custom, for example "xyz". I added javax.faces.validator.BeanValidator.MESSAGE=xyz line to MyMessages.properties and nothing changedKrzysztof Tomaszewski
I'm answering myself: to set custom message for required validation you need to set in message-bundle 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) 2) javax.faces.component.UIInput.REQUIRED - a message that is presented when showSummary=true in h:message and h:messages tags NOTE: this is not true that javax.faces.validator.BeanValidator.MESSAGE is responsible for required-field-error messages.Krzysztof Tomaszewski

2 Answers

2
votes

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.

0
votes

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);