This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 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

How to validate a form when BindingResult can't be a parameter

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know you can easily (and almost without any effort) validate a form with the following controller:


But now I want to execute the same validation in the following method:


I know that seems a bit weird. But because data can be saved as in written to the database (even if there are validation errors) it's a functional requirement to show possible validation errors when the form is initially requested.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bill Gorder wrote:Just @Autowire in a Validator and call validate on the form bean

http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#validation-beanvalidation-spring-inject



I guess you are referring to the javax.validation.Validator? Not an option at this moment, because the validation is already completely written using Spring's validator

But for future reference: I wonder if I use the bean validation api how the validation errors are propagated back to the jsp, so these errors can be shown using errors-tag?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After some google research I have found a solution which seems to work very well after initial testing.

According to the Spring reference you can configure a DataBinder programatically. So you can set a validator and validate the target using this validator. This gives you a BindingResult (containing possible validation errors). Then you have to simply add that BindingResult instance as an attribute to the model (using the correct name) and you can access the errors in the JSP using errors-tag. A deeper look into the source code of the errors-tag reveals the name of the attribute we must use (and it's really easy: BindingResult.MODEL_KEY_PREFIX + modelName)

So combining all this information in the read-method results in the following code with same (similar) behavior with the update-method using the @Valid annotation
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic