• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic