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

后端 未结 3 568
庸人自扰
庸人自扰 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 02:59

    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.

提交回复
热议问题