aspose file tools*
The moose likes JSF and the fly likes JSF validation messages ... how to customize ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF validation messages ... how to customize ?" Watch "JSF validation messages ... how to customize ?" New topic
Author

JSF validation messages ... how to customize ?

Alex Nedelcu
Greenhorn

Joined: Feb 06, 2006
Posts: 15
Hi,

I'm still learning JSF ... sorry for such a newbie question.

When having validators on fields, how can I customize the error messages, but for each field in particular.

I know about overwritting javax.faces.component.UIInput.REQUIRED, but I don't want my message to sound so general, like `Validation Error: Value is required`.
I want my error message to be customized for each field, like:
Username is required
Password is required
Please repeat password
Alex Nedelcu
Greenhorn

Joined: Feb 06, 2006
Posts: 15
Unfortunately I found that the answer is not so beginner-friendly.

According to this article
http://www.oracle.com/technology/pub/articles/masterj2ee/j2ee_wk7.html
one can customize these error messages by implementing a phase listener.

I'm a little dissapointed.
Another hack like this for such a simple thing, and I'll dismiss JSF completely, although I do feel a relief that at least it is possible.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Hi Alex,

I'd be interested in seeing how others do this but this is my approach:

1. Instead of using required="true" (because I don't know how to override that bit of behaviour), I wrote a custom converter:

I won't go into how to write and register a custom converter here. Any JSF book will show how to do this. (Why not a validator? Validators are not called on empty fields.)

2. I use the id of the component as the message key for the label text, so in my message bundles I have:

firstname=First name
...
msg_something_required={0} is required

3. In the converter, the code goes along these lines:

Pulling strings out of message bundles is harder than it should be in JSF, but again, any JSF book will have an example.

4. If you don't want to use the id as a key, you can use an attribute:


5. One fly in the ointment is if you want to use another converter, say to convert a Date. In that case I subclass my RequiredConverter.


There is no emoticon for what I am feeling!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I just checked out that reference. I'd say it's a good solution, probably better than what I just suggested, because it doesn't use up the converter attribute. As for it being a hack, welcome to JSF. A lot of things have to be done like that.
Sergio W. Del Valle
Greenhorn

Joined: Feb 13, 2006
Posts: 3
Here's a simpler solution using resource bundles, don't worry if you're not using the JSC2 IDE, the files involved can be edited and/or created on whatever IDE you're using.

http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/custommessage.html
Sergio W. Del Valle
Greenhorn

Joined: Feb 13, 2006
Posts: 3
Ooops, sorry my hint won't work for displaying a different message for each field.
ernest fakudze
Ranch Hand

Joined: Aug 27, 2001
Posts: 216
Hi guys,

I found a handy way of doing this from the J2EE totorial at http://java.sun.com/j2ee/1.4/docs/tutorial/doc/ . See the section "Localizing Dynamic Data". The method that I'm now using is listed below:



NOTES

The @param clientId is the unique id of the component as known by the UiViewRoot. So if your form is called testForm and your input field is called firstName the client id would be testForm:firstName.

The @param key is the name of the key that you want to look up from the bundle. It could be something like formFirstNameRequired,


I would recommend creating a utility bean for doing everyday stuff and stick this method there. Make this a managed bean with application scope. Then, your component ( e.g. inputField) will have it's validator hooked up to some method in another bean and that method will use the method above to validate the component and then add an error message for the component when it fails validation.

I've used this technique and it works great. Here is my method which does the validation below...please note for testing I jsut had both mthods in the same bean

public void validateFirstNameInputField() {
FacesContext thisContext = FacesContext.getCurrentInstance();
Map requestParams = thisContext.getExternalContext().getRequestParameterMap();
String firstName = (String) requestParams.get("testForm:firstName");

//Test for appropriate length of the field and add appropriate error message
if( firstName.length() == 0 ) {
message("testForm:firstName", "formFirstNameRequired");
//in my bundle this key has the value: First name is required
}
}


I hope that helps!

Ernest

[ February 28, 2006: Message edited by: ernest fakudze ]
[ February 28, 2006: Message edited by: ernest fakudze ]

In a time of drastic change it is the learners who inherit the future. The learned usually find themselves equipped to live in a world that no longer exists.<br />Eric Hoffer
torben morello
Greenhorn

Joined: Jul 08, 2008
Posts: 1
With JSF 1.2 you can use the following snippets to get it to work... the attribute label of element h:inputText sets the input specific text of the message (and is only available since 1.2 as far as I know)...

------------------------------ faces-config.xml
<application>
<message-bundle>com.myproject.messages</message-bundle>
</application>
------------------------------ messages.properties
javax.faces.component.UIInput.REQUIRED=Please enter a value for {0}
javax.faces.validator.LengthValidator.MINIMUM = At least {0} characters are required for {1}

------------------------------ mytemplate.jsp
<hutputText value="User" />
<h:inputText id="username" value="#{loginBean.username}"
required="true" label="User">
<f:validateLength minimum="3" />
</h:inputText>
Andres Quinones
Ranch Hand

Joined: Oct 09, 2006
Posts: 57
Which JSF implementation are you using? Because as far as I have researched with MyFaces 1.2 you can put your own messages for each component.

Hope this helps.
[ July 08, 2008: Message edited by: Andres Quinones ]
Venkat Sadasivam
Ranch Hand

Joined: May 10, 2008
Posts: 139
If you are using JSF 1.2, requiredMessage, converterMessage, validatorMessage attribute will help you to define customized error message for each field.

Example:
<h:inputText value=�#{bean.value}� requiredMessage=�#{bundle.requiredMessage}� converterMessage=�#{bundle.converterMessage}� validatorMessage=�#{bundle.requiredMessage}�>


“Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”<br>
-Martin Fowler
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF validation messages ... how to customize ?