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 :

  1. Spring 4.1.6.RELEASE
  2. 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.

References

  1. Validation, Data Binding, and Type Conversion
  2. Validating Form Input
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Kemal
Guest
Kemal

We can use binder.addValidators(new UserFormValidator()); in initBinder method. Then both spring validator and hibernate validater will work together.

Norayr
Guest
Norayr

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 .

Mad Dy
Guest
Mad Dy

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?

Hung
Guest
Hung

did you resolve it?

Ro
Guest
Ro

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.