• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Problem with bindingResult.hasErrors() in Spring Boot with Thymeleaf.

 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following Form:

I want to display user errors on input for Address, City and State; The class LicensesRequest has these properties with getters and setters:

The code in the controller is:

The problem is that bindingResult.hasErrors() is always false.
 
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is how I did it.  I think you are missing @ModelAttribute

 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you TIM, I changed my code to

but I  still have "bindingResult.hasErrors()" false when I don't send any data.
The form is

LicenseRequest is

All properties in are coming in as null.
 
Alejandro Barrero
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Resolved. But you are not going to believe it; coping code from samples in the Internet, I had to use @Valid  and searching I discover that I needed "javax.validation.Valid",  searching how to get it with Maven I ended using

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>

and I had no compilation errors. Keeping looking at samples in the Internet I found and application that wasn't using the above and so far, it is not clear to me how is it doing it. However, I found a page clarifying that earlier, "spring-boot-starter-web" supported "javax.validation.Valid", but that had been removed and the dependency to be used is

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest -->

Well, my application works now and I thank you all and bid you well.
 
Bartender
Posts: 2266
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, you may find this stackoverflow thread helpful https://stackoverflow.com/questions/47658774/bindingresult-always-false-spring-mvc
As someone suggested, add hibernate-validator dependency to your pom.
 
Himai Minh
Bartender
Posts: 2266
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Alejandro,
In spring-boot-starter-web artifact , there is a hibernate-validator that does the validation.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use this dependency
reply
    Bookmark Topic Watch Topic
  • New Topic