1

How can I create a custom message for the errors that accour in the field annotation?

@Size(max = 10)

or

@Column(name = "NAME"  , length = 10) 

Now I see a message

CustomerDetailsForm:customerName: Validation Error: Length is greater than allowable maximum of ''10'' .

How can I change this message?

1 Answer 1

2

Use the message attribute of the bean validation annotation.

@Size(max=10, message="May not be more than 10 characters.")

To internationalize it, supply ValidationMessages.properties bundle files in the desired locales and use the {} to specify the bundle key, e.g:

@Size(max=10, message="{validation.max_size}")

Alternatively, you can also just use maxlength on the <h:inputText> field so that the enduser already won't be able to enter too much characters.

<h:inputText ... maxlength="10" />
Sign up to request clarification or add additional context in comments.

13 Comments

thank's! I want to internationalize it like you said.. what do you mean with ValidationMessages.properties bundle files? do I need to create ValidationMessages.properties?
do i need to define ValidationMessages.properties inside something? and when i call it's message it should be "{validation.max_size}" ? thank's again.
Just put the files in the root of the classpath. The properties can be just validation.max_size = May not be more than 10 characters. The key and value is fully free to your choice. The {} just indicate that the message should be looked up from the bundle by the specified key.
'Just put the files in the root of the classpath' - you mean the ValidationMessages.properties file right? are there any more files that should be added? I will try it now, thank's!
One file per locale. E.g. ValidaitonMessages_en.properties, ValidationMessages_de.properties, etc. See the ResourceBundle javadoc. It's not different from all other "usual" resource bundle approaches. If you don't need localized messages or want a default locale, you can also just supply alone ValidationMessages.properties.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.