Home > customize validation > customize validation error message in jsf 2.0

Customize Validation Error Message In Jsf 2.0

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to customize JSF validation error message up vote 14 down vote favorite 3 How can I customize the validation message that appears when validation fails? Here is the code I have:

Currently the message looks like this: j_idt10:userNo: Validation Error: Specified attribute is not between the expected values of 3 and 6. Which is not particularly user-friendly. validation jsf message facelets share|improve this question asked May 2 '12 at 10:18 Eleeist 2,04962856 add a comment| 3 Answers 3 active oldest votes up vote 27 down vote accepted The simplest way would be to set the validatorMessage="my custom message" attribute in the tag. For a more advanced way read this article Customize validation error message in JSF 2.0 And here a complete Reference to all available message that you can override in JSF 2.0.x share|improve this answer edited May 12 '14 at 20:09 shybovycha 4,35922966 answered May 2 '12 at 10:23 Daniel 27.4k352102 It is surprising to me that there is no way to set the message "on-the-fly" in an attribute or something. Huh... –Eleeist May 2 '12 at 10:26 there is , edited my answer... –Daniel May 2 '12 at 10:26 2 That definitely makes more sense. But what if I have more than on

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to set a custom message for a standard validator in JSF? http://stackoverflow.com/questions/10411773/how-to-customize-jsf-validation-error-message up vote 6 down vote favorite 4 I created a simple input field for an int variable. I want to add validation to it but, but i see no need to create my own validator for it, since the only thing i need is to avoid negative values and characters other than numbers. So this is what i did:
http://stackoverflow.com/questions/6250685/how-to-set-a-custom-message-for-a-standard-validator-in-jsf When i try how it works this is the result for negative values: And this is the result for characters: Where in my project can i customize the text of those validation messages? java validation jsf java-ee jsf-2 share|improve this question edited Jun 6 '11 at 10:35 asked Jun 6 '11 at 10:27 sfrj 6,37319101171 add a comment| 2 Answers 2 active oldest votes up vote 16 down vote accepted To change these values you basically have to define your own Resources file, override the properties you want custom messages for and register it in web.xml. This link seems to explain it well, so I've included this rather than try and rewrite the content. For a simpler solution and as you are JSF 2 you can also use these attributes of the inputText component: requiredMessage="I am a custom message" converterMessage="I am a custom message" validatorMessage="I am a custom message" This will override any message which the validator itself spits out. But I'd prefer the Resource bundle, as that promotes consistency of user visible error messages across your application. share|improve this answer edited Jun 6 '11 at 10:48 answered Jun 6 '11 at 10:38 planetjones 8,26213045 Thank you

New Topic programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums Forum: JSF JSF validation messages ... how to customize ? Alex Nedelcu Greenhorn Posts: 15 https://coderanch.com/t/211313/JSF/java/JSF-validation-messages-customize posted 10 years ago 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 customize validation password Alex Nedelcu Greenhorn Posts: 15 posted 10 years ago 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 Posts: 1780 customize validation error posted 10 years ago 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: public Object getAsObject(FacesContext context, UIComponent component, String string) { if (string==null) { String id = component.getId(); //"firstname" String label = ... lookup id in message bundles //"First name" String message = ... lookup msg_something_required in message bundles and perform formatting //"First name is required" throw converter exception } return string; } 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: public Object getAsObject(FacesContext context

 

Related content

customize validation error message in jsf 1.2

Customize Validation Error Message In Jsf p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this relatedl site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of million programmers just like you helping each other Join them it only takes a minute Sign up How to customize JSF validation