简体   繁体   中英

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.

for 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.

You have two way to solve this situation.

  1. You may remove @NotNull . @Pattern annotation also does not accept null value and modify your error message.

  2. Create custom validation annotation which combine two annotation in one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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