Main Tutorials

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

About Author

author image
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.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Kemal
3 years ago

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

Norayr
6 years ago

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
7 years ago

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?

shiva
3 years ago
Reply to  Mad Dy

do you solve it ? i stuck in this for weeks now 🙁

Hung
5 years ago
Reply to  Mad Dy

did you resolve it?

Ro
8 years ago

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.