0

I was looking up on how to perform validation in Spring MVC.

Manually call Spring Annotation Validation

Spring MVC: How to perform validation?

The above to links are couple of those. What I was wondering is that calling the Validator.validate manually is considered a bad practice compared to that of using @Valid/@Validated annotations ?

Novice
  • 423
  • 1
  • 9
  • 17
  • If you have very limited and fairly simple validations you can use annotations. In case of more complex and lengthy validations, use of `Validator.validate()` could be preferred. – VPK Oct 11 '17 at 06:26
  • I disagree with @VPK, the right way to do it if you a couple of simple validations it's using the `@Valid` and binding a Validator to your pojo class through `@InitBinder`. With this you don't have to call the `validate()` and the only thing that you have to do is to call hasErrors(). If you have for some reason to add something to your class that you want to validate, you should call validate manually and don't using the `@Valid` and the `@InitBinder`. – voliveira89 Oct 11 '17 at 09:27
  • @voliveira89, I know that you can bind the validator using the `@InitBinder` and then you don't need to call validate method manually. Even annotations work well enough. But it does not provide more control as to when and how your bean should be validated. Depending on the situation you may want to modify your bean before validation. In which case, other two methods won't be useful. – VPK Oct 11 '17 at 09:46
  • @VPK so we agree after all :) At first I only disagree with because the user that post the question don't specify why he want to call a validator manually. – voliveira89 Oct 11 '17 at 09:52
  • @voliveira89, yes, in a way. ;) But the actual point is in which situation what method should be used. So it is very hard to choose one method for each situation. – VPK Oct 11 '17 at 09:54

0 Answers0