• 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

Creating an instance of Errors interface.

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I was wondering if there would be any way to create an instance of the "org.springframework.validation.Errors" object.
I know that Errors is an interface.
The problem is, I have got a validator class that I would like to reuse for my other code.(by calling the validate() method).

I know the right way would be to separate the validation logic from the validator so that the logic can be used wherever.

But right now I am trying to provide something quick and dirty, so was wondering about doing this.

Thanks in advance,
Chinmay
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create a DataBinder for the object you are validating, create a MutablePropertyValues object, set the values you want to set on the object by calling mutableProperties.add("propertyName", value), call dataBinder.apply(mutablePropertyValues) to attempt to bind the values to the target object, then call dataBinder.getBindingResult() - this returns an instance of a BindingResult, which also implements the Errors interface. The BindingResult returned originally contains any errors that occurred during the bind, like any conversion/casting errors on the values, but now you can also use the BindingResult to use in your Validator's validate(target, errors) method.

 
reply
    Bookmark Topic Watch Topic
  • New Topic