*
The moose likes JSF and the fly likes replacing default validator message at h:messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "replacing default validator message at h:messages" Watch "replacing default validator message at h:messages" New topic
Author

replacing default validator message at h:messages

Pamungkas Timotius
Greenhorn

Joined: May 22, 2005
Posts: 26
Having this code




and a <h:messages> in corresponding JSP, can I replace default validator's message?

For example, instead of displaying "registerNewAccountFormOne:txtRequestedUserName: Validation Error: Value is less than allowable minimum of '6'", which will confusing for my end-users, can I display "user name too short (minimum 6 characters)"

Also, at the same time, can I customize validation for which attached on same input component (txtRequestedUSerName in this case).

Thanks..
David Heffelfinger
author
Ranch Hand

Joined: Jul 08, 2004
Posts: 176
Pamungkas,

A custom error message for length validation can be achieved by creating a property file with a key of:



Then adding the following tags your faces-config.xml:



In the above example, replace com.mycompany with the correct package name, and MyMessageBundle with the file name you gave your property file.

A similar technique can be used for customizing the required field validation message.

This technique is covered in detail in my book, Java EE 5 Development With GlassFish Application Server

Hope that helps,
David
[ October 29, 2007: Message edited by: David Heffelfinger ]

Author, <a href="http://www.packtpub.com/java-ee5-development-with-netbeans-6" target="_blank" rel="nofollow">Java EE 5 Development with NetBeans 6</a>
Pamungkas Timotius
Greenhorn

Joined: May 22, 2005
Posts: 26
Thanks for your information, I'll try it at once
Pamungkas Timotius
Greenhorn

Joined: May 22, 2005
Posts: 26
Yes, it works...
But I'm expecting something more specific and user-friendly

For example, if user does enter password which length is less than 6 chars, my message should display 'Password must be at least 6 characters'
but when username entered is less than 5 chars, it must displays proper message : 'Username must be at least 5 characters'

I've tried this approach in properties file


but it displays component id (e.g. newUserForm:txtUsername)

Can I display custom text instead of component's id in <h:messages> ?

Thanks
 
Consider Paul's rocket mass heater.
 
subject: replacing default validator message at h:messages