This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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: 92
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: 2119
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: 2119
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
 
Space pants. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic