Combine Spring validator and Hibernate validator
In this article, we will show you how to validate the submitted form values with Spring validator and Hibernate Validator (bean validation).
Technologies used :
- Spring 4.1.6.RELEASE
- Hibernate Validator 5.1.3.Final
1. Hibernate Validator
The Hibernate validator will be fired if @Valid
is provided.
import org.hibernate.validator.constraints.NotEmpty;
public class User {
@NotEmpty
String name;
//...
}
@RequestMapping(value = "/users", method = RequestMethod.POST)
public String saveOrUpdateUser(
@ModelAttribute("userForm") @Valid User user,
BindingResult result, Model model) {
if (result.hasErrors()) {
//...
} else {
//...
}
}
2. Spring Validator
If you enabled the Spring validator via @InitBinder
, the Hibernate bean validation will be ignore.
public class UserFormValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
User user = (User) target;
//validate something else
}
}
@Controller
public class UserController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new UserFormValidator());
}
3. Hibernate Validator + Spring Validator
To have both Hibernate and Spring validator. Remove the @InitBinder
and fire the Spring validator manually.
@Controller
public class UserController {
/*@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new UserFormValidator());
}*/
@RequestMapping(value = "/users", method = RequestMethod.POST)
public String saveOrUpdateUser(
@ModelAttribute("userForm") @Valid User user,
BindingResult result, Model model) {
//run Spring validator manually
new UserFormValidator().validate(user, result);
if (result.hasErrors()) {
//...
} else {
//...
}
}
In the above example, the submitted “userForm” model will first validate by Hibernate validator, then follow by Spring validator.
We can use
binder.addValidators(new UserFormValidator());
in initBinder method. Then both spring validator and hibernate validater will work together.What if i have an
@Column(name = “fixedAsset_inventoryNumber”, unique = true, nullable = false)
@NotEmpty(message = “inventory number is requrequired “)
private String inventoryNumber ;
and my inventoryNumber must be unique .
How can i validate it and show message in my thymeleaf .
Unfortunately my code is not working. Somehow, BindingResult.hasErrors() always returns false. No matter what is the form input is. I tried to check the values of form using result object. I am getting the values just as entered. But, any hibernate annotations are not validated by the code. Can you please help me in this matter?
do you solve it ? i stuck in this for weeks now 🙁
did you resolve it?
im using spring 4.1.7 version and hibernate 5.2.1 validator version. But when i uncomment new UserFormValidator().validate(user, result); only spring validation is executed, Hibernate validation is skipped. So dont understand why its not working both.