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

后端 未结 3 566
庸人自扰
庸人自扰 2021-02-04 02:41

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         


        
3条回答
  •  不思量自难忘°
    2021-02-04 03:01

    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.

提交回复
热议问题