It's not a secret anymore!
The moose likes Spring and the fly likes Creating an instance of Errors interface. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Creating an instance of Errors interface." Watch "Creating an instance of Errors interface." New topic

Creating an instance of Errors interface.

Chinmay Bajikar
Ranch Hand

Joined: Dec 08, 2001
Posts: 159
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,

The strength of the Wolf is the pack & the strength of the pack is the wolf....Rudyard Kipling
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

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.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Creating an instance of Errors interface.
It's not a secret anymore!