Mathew
Mathew

Reputation: 11

BindingResult always return 0 errors Hibernate Validator

I have problem with Hibernate Validator or more precisely with BindingResult and method .hasError(). It return always true even with null object. Check my code here: https://github.com/jeddyn/spring-mvc-demo

Customer: null customer first name: null
bindingresult: org.springframework.validation.BeanPropertyBindingResult: 0 errors
Customer: null customer first name: null
bindingresult: org.springframework.validation.BeanPropertyBindingResult: 0 errors

Upvotes: 1

Views: 1729

Answers (2)

Kevin O.
Kevin O.

Reputation: 446

For me it works this way:

    @PostMapping("/registration/post")
    public String processRegistrationForm(@ModelAttribute("registrationForm") @Valid RegistrationForm registrationForm, BindingResult bindingResult, HttpServletRequest request, RedirectAttributes redirectAt) {...}

Before bindingResult was always 0 because I had forget to add spring-boot-starter-validation the pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Upvotes: 0

Alien
Alien

Reputation: 15908

Try adding below dependency in pom.xml

<dependency>
     <groupId>javax.validation</groupId>
     <artifactId>validation-api</artifactId>
     <version>1.0.0.GA</version> 
</dependency>

You can change the version compatible with hibernate validators.

Put @Valid before @ModelAttribute by changing order like below, weird but it works.

public String processForm(
            @Valid @ModelAttribute("customer") Customer customer,
            BindingResult theBindingResult)

Refer this

Upvotes: 1

Related Questions