简体   繁体   English

Spring Boot + JPA + Thymeleaf + Hibernate多个验证注释已应用

[英]Spring Boot + JPA+Thymeleaf+Hibernate multiple validation annotation applied

Before writing my problem, i have search a lot on internet. 在写我的问题之前,我在互联网上进行了大量搜索。 But i did not found any clue to overcome this issue. 但是我没有找到解决这个问题的任何线索。

My problem is multiple validation annotation applying of POJO field. 我的问题是POJO字段的多个验证注释应用。

for Exp: 对于Exp:

@NotNull
@Pattern(regexp="(^[0-9]{10})")
private String mobileNumber; 

If i do not enter any thing and left blank, then both the validation applied and both messages displayed. 如果我没有输入任何内容并留空,那么将同时应用验证并显示两条消息。 I just want one validation applied at a time and only one message display at a time. 我只想一次应用一次验证,一次只显示一条消息。

I am using Spring boot and thymeleaf as template engine. 我正在使用Spring boot和thymeleaf作为模板引擎。

You have two way to solve this situation. 您有两种方法可以解决这种情况。

  1. You may remove @NotNull . 您可以删除@NotNull @Pattern annotation also does not accept null value and modify your error message. @Pattern注释也不接受空值并修改您的错误消息。

  2. Create custom validation annotation which combine two annotation in one. 创建将两个注释合而为一的自定义验证注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM