It's not a secret anymore!*
The moose likes JSF and the fly likes How to display validation error messages without component ID Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to display validation error messages without component ID" Watch "How to display validation error messages without component ID" New topic
Author

How to display validation error messages without component ID

Harpreet Parmar
Greenhorn

Joined: Nov 10, 2010
Posts: 9
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.


Thanks,
Harpreet Singh
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16070
    
  21

In JSF 1.2 and later, you can set a validationMessage attribute on the control and override the default message.


Customer surveys are for companies who didn't pay proper attention to begin with.
Cesar Loachamin
Ranch Hand

Joined: Dec 25, 2010
Posts: 90

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


When a dream is ending because to come true - OCPJP 6,7. OCE JPA EE6. MCTS
Harpreet Parmar
Greenhorn

Joined: Nov 10, 2010
Posts: 9
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

Joined: Jun 25, 2001
Posts: 16070
    
  21

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

Joined: Dec 25, 2010
Posts: 90

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display validation error messages without component ID