Multiple Regex @Pattern's for 1 Field?

前端 未结 4 519
天涯浪人
天涯浪人 2020-12-30 03:15

I was attempted to apply multiple @Pattern annotations to a single field:

@Pattern(regexp = \"(?=.*[0-9])\", message = \"Password must contain one digit.\")
         


        
4条回答
  •  醉话见心
    2020-12-30 03:46

    You might want to look into constraint composition. You would build one annotation per password constraint composed from the @Pattern annotation, and then finally build one composed annotation using the previously defined four. This would require no extra java code.

    http://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html_single/#validator-customconstraints-compound

    Otherwise, build (with accompanying java code) a custom annotation called @PasswordValidator.

    I tend to like composed validators, as it makes the purpose of the validation annotations clear on their purpose, instead of having many disjoint annotations.

提交回复
热议问题