0

I'm just wondering about data validation in Spring. Spring is offering two (maybe more) validation options. First option is to use Validator interface and create whole validation on my own. Second option is to use annotation validation (JSR 303).

And now I'm really confused :) which validation I should chose. What I need is to check if recieved Data Object is correct (by correct i mean all required fields are filled) and this can be done by JSR 303 validation or by my own validator with "Validator instance". But I also need to check if is this Data Object valid against some database constraints (validator is required to check some data in database, eq. is user with this ID registered or not ...) and this can be done only by my own validator.

I don't know which way should be the best. Combine both or create my own validator ?

I will be thankful for any advice ...

Update (relocated from comments)

Ok, I followed Ryan's example and I think I was successful. I created my own implementation of spring Validator and in this Validator I @Autowire-d javax JSR 303 instance. But there was problem with that injection. I had in my configuration and this piece of code caused some exceptions, because spring did not know which Validator I want to inject. So I removed that code.

At the end I also removed the spring Validator implementation, because I dont know where I can get Errors property, which is required as second parameter in "validate" method :). I'm triggering that validation manually from service layer and I really don't know, where I can obtain that Error object.

BTW Well, I found another solution how to implement that validation. I'm thinking about to extend my validator class by LocalValidatorFactoryBean. LocalValidatorFactoryBean class implementing both Validator interfaces (Spring Validator + JSR 303). But i'm not sure if is this good approach. This approach also require Error object, which I don't know where to find/get.

Update The Error object is coming from "BindException".

FooObjectVO myObject = new FooObjectVO();
BindException errors = new BindException("fooObject", myObject);

2 Answers 2

1

They're not really separate options--more like two available implementations of validation. Since Spring's JSR 303 support also implements its own Validator interface, you don't really have to pick one or the other. You can mix and match the implementations in whatever way makes it easiest to get the job done.

Sign up to request clarification or add additional context in comments.

7 Comments

I'm not sure if I understand you right. Are you refering to combine the two implementations into one by implementing that Validator interface? Or are you referring to create my own annotation constraint ? I'm sorry if my comment is a little bit dumb, but I'm newbie in validation with spring (well, in spring at all). Please, can you provide me a small example or small guide how to combine JSR 303 and my own validation ?
Well, one way would be to write your own implementation of Spring's Validator interface and inject it with an instance of Spring's JSR 303 Validator. Then you invoke the JSR 303 validator for basic field validation and then do your own custom validation.
@Peter: When you have an update to your question (or a new question), it shouldn't be posted in comments. I've moved your update up to your question, and I'm flagging your comments for removal.
Ah, I did not know that, thank you :) BTW Just small question, is my described approach correct ? Well, it works, but I'm looking for the right solution not only the "It works" solution :). I really don't like messy code.
Extending LocalValidatorFactoryBean probably isn't a good idea, if that's what you did. As for not knowing where to get an Errors object, that's not something you should deal with. Spring invokes Validators and passes objects to it. Finally, the first thing you mentioned seemed to be the best way, but you gave up when Spring complained about ambiguous bean definitions, when you should have just used a qualifier to specify what you wanted where.
|
0

In cases such as this I prefer to combine both. I like to use JSR 303 validation for whatever I can. I supplement it with my own validators.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.