Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Validator - @Length - How to specify separate message for min and max?

Tags:

I am using Hibernate validator for form validation in my web-app. I am using the @Length annotation for my String attribute as follows:

@Length(min = 5, message = "The field must be at least 5 characters")
private String myString;

However, I have a need to display a different message if the String exceeds 50 characters. Is there a way to use the out-of-the-box @Length validator to do this? An example of what I would like to do (compiler will not let me) is as follows:

@Length(min = 5, message = "The field must be at least 5 characters")
@Length(max = 50, message = "The field must be less than 50 characters")
private String myString;

I have tried @Max and @Min and they do not do what I want. Any input would be greatly appreciated!

like image 513
javagruffian Avatar asked Aug 16 '11 20:08

javagruffian


People also ask

What is the difference between javax validation and Hibernate Validator?

The Javax bean validation API provides the following most frequently used annotations. The Hibernate validator provides the following commonly used annotations for validation. In case of product or project development we must use both the annotations for bean validation.

Is Hibernate Validator thread safe?

Validating constraints In the setUp() method, a Validator instance is retrieved from the ValidatorFactory . Validator instances are thread-safe and may be reused multiple times.

What is the use of Hibernate Validator dependency?

Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.

What is @size in spring boot?

@Size(min=2, max=30) : Allows names between 2 and 30 characters long.


2 Answers

You can specify several @Length constraints at one element by using the inner list annotation (which is defined for each constraint type in Bean Validation/Hibernate Validator) like this:

@List({
    @Length(min = 5, message = "The field must be at least 5 characters"),
    @Length(max = 50, message = "The field must be less than 50 characters")
})
private String myString;

Btw. I recommend to prefer @Size as defined by the Bean Validation API over @Length for portability reasons.

like image 165
Gunnar Avatar answered Sep 17 '22 19:09

Gunnar


If you use the java standard javax.validation.constraint.Size, you can achieve the same thing this way:

@Size.List ({
    @Size(min=8, message="The field must be at least {min} characters"),
    @Size(max=60, message="The field must be less than {max} characters")
})
private String myString;

Notice that you can avoid hardcoding the field size by using message interpolation.

like image 35
Chanoch Avatar answered Sep 21 '22 19:09

Chanoch