• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Ranch Hand
Posts: 494
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Here is how I did it.  I think you are missing @ModelAttribute

 
Alejandro Barrero
Ranch Hand
Posts: 494
  • Mark post as helpful
  • send pies
  • 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: 494
  • Mark post as helpful
  • send pies
  • 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: 1945
13
  • Mark post as helpful
  • send pies
  • 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: 1945
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Alejandro,
In spring-boot-starter-web artifact , there is a hibernate-validator that does the validation.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic