This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to display validation error messages without component ID

 
Harpreet Parmar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using JSF 1.2 with Facelets. I use <h:message> to display error for each input field.
Whenever I get an error message for built-in kind of validations, an id gets appended to it. E.g. I have some Integer (Wrapper type) type of property in my backing bean and corresponding text box on my page, whenever user enters a value that is beyond the range of "int" or is not an "int", the error message that gets displayed is like:
"j_id50:j_id54:program_creditDueDate: 'ssd' must be a number between -2147483648 and 2147483647 Example: 9346"

This does not happen for manually defined validations using Hibernate Validator annotations like @Range, @Length.

I want to remove this component id part from the error message, how can I do that?

Thanks in advance.

 
Tim Holloway
Saloon Keeper
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JSF 1.2 and later, you can set a validationMessage attribute on the control and override the default message.
 
Cesar Loachamin
Ranch Hand
Posts: 90
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harpreet

Since JSF 2.0 every component that implements the ValueHolder interface (<h:input />) has a label attribute, this attribute is used to show the name of the component in the message when a error converter or error validation occurs, if you don't specify the label attribute the jsf implementation takes the name of the component (is formed for the id of the containers parents and the id of the component separte by colons) in your case j_id50:j_id54:program_creditDueDate, if you want to remove this id you can set the label attribute to white space, but I suggest you put a description of the component like this:

This shows a message like this: "Due Date: 'ssd' must be a number between -2147483648 and 2147483647 Example: 9346"
I hope this help you with your issue.
Regards
Cesar
 
Harpreet Parmar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cesar and Tim for your help.
I tried that "label" attribute and it works. But if I set it to a white space, a colon still appears, that doesn't look right. I want to remove it completely, is there any way to that?

Tim, can you please elaborate a little on your reply? I couldn't find validationMessage attribute on h:inputText. I am not very sure what you meant there. I am new to JSF.

Thanks
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harpreet Parmar wrote:Thanks Cesar and Tim for your help.
I tried that "label" attribute and it works. But if I set it to a white space, a colon still appears, that doesn't look right. I want to remove it completely, is there any way to that?

Tim, can you please elaborate a little on your reply? I couldn't find validationMessage attribute on h:inputText. I am not very sure what you meant there. I am new to JSF.

Thanks


That's because I relied on my rotten memory. It's actually "validatorMessage".

http://download.oracle.com/javaee/5/javaserverfaces/1.2/docs/tlddocs/h/inputText.html
 
Cesar Loachamin
Ranch Hand
Posts: 90
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harpreet

When you're using a type like integer in your model and your bind to the value of the inputText, the jsf implementation use a implicit converter, for your case the jsf-impl use the javax.faces.convert.IntegerConverter whose converter-id is javax.faces.Integer, beacuase you have a converter error show in the <h:messeges> not is a validation error, for that you must not use the validatorMessage you must use the converterMessage attribute in the inputText to override the message generted by the jsf-impl.

You have another option you can change the text of Standard Error Messages, but you need to create a message bundle and in this create the messages for all the error messages that use jsf. I think the first option is okey to solve your problem.

Regards
Cesar.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic