Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a custom message for a standard validator in JSF?

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:

<h:inputText id="price" value="#{placeAddControler.price}">
            <f:validateLongRange minimum="0"/>
</h:inputText>          
<h:outputText value="€" />
<br/>
<span style="color: red;"><b><h:message for="price"
showDetail="true" /></b></span>

When i try how it works this is the result for negative values: enter image description here

And this is the result for characters:

enter image description here

Where in my project can i customize the text of those validation messages?

like image 943
javing Avatar asked Jun 06 '11 10:06

javing


People also ask

How to validation in JSF?

JSF Validation - Declarative Validator Here we are setting the required attribute to true which makes the field mandatory and fires the custom message “value is required” for color field and user defined message for mobile name field as the message is specified in the requiredmessage attribute.

Which facelets core tag is used to validate string length to a particular range?

f:validateLength tag is used to validate the length of a string value in a particular range.


2 Answers

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.

like image 80
planetjones Avatar answered Oct 15 '22 03:10

planetjones


You can register a message bundle and customize those messages. This is a pretty decent example/tutorial:

http://www.mkyong.com/jsf2/customize-validation-error-message-in-jsf-2-0/

like image 34
Dave Maple Avatar answered Oct 15 '22 01:10

Dave Maple