views:

430

answers:

2

Hi,

I am starting a new Spring-MVC project. Validation is a important feature in any project. This way, i have seen many approachs as follows:

  • Spring Validator interface

It sounds like a Struts 1.x validation. In my opinion, it is not agile.

  • Commons Validator

I think it is better than Validator interface

  • Annotation based bean validation (Spring modules)

It appears to be agile, but i do not know whether it does support for collection-based property

So, what's the most AGILE APPROACH to validate Spring-MVC command in your opinion ? And why ? Consider many features like collection-based property, custom validation etc. If you know/use another approach in Spring-MVC, feel free to share it.

regards,

+2  A: 

"Agile" means different things to different people :-)

That said, Hibernate Validator is very convenient if you're using Hibernate as ORM because it allows you to specify your constraints ONCE and have (some of) them propagated to database, work in your business layer and work in your UI layer. Additionally, 4.0 beta implements JSR 303 which most likely will be the standard approach to validation going forward.

Validator 3.1 does have certain issues with automatically validating constraints within collections of elements, but it's reasonably easy to patch and / or work around (been there, done that, works perfectly now). Haven't migrated to 4.0 beta yet, so I don't know whether that was fixed.

It's definitely extensible - you can easily write your own constraints.

ChssPly76
@ChssPly76 Hi, do you use it successfully in Spring MVC ? regards
Arthur Ronald F D Garcia
I use it in conjunction with Spring, not just in UI layer (which is based on Spring MVC) but throughout the entire system. DB schema is generated based on Validator annotations (e.g. column length for text fields is derived from `@Length` instead of being set via `@Column`), business layer entities are using the very same constraints, UI decorators (where applicable) are annotated with them as well. Works great. I have a few custom validators too.
ChssPly76
A good introduction is found here: http://www.sleberknight.com/blog/sleberkn/entry/20070910 Six in-depth articles about Hibernate Validator.
Arthur Ronald F D Garcia
+2  A: 

How is implementing a Spring Validator not agile? You are able to enter the Validation logic yourself. How hard is this?

public void validate(Object target, Errors errors) {

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email",
     "error.blank.email", "Email address is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", 
     "error.blank.name", "Name is required");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone",
     "error.blank.phone", "Phone is required");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "question", 
     "error.blank.question", "Question is required");
}

Very agile!

matt b
If you look at the code in your sample, you'll notice that the only difference between those 4 `ValidationUtils` calls is the field name and it's repeated twice (three times counting the message, but that should be localized anyway). Using Hibernate Validator you'd just annotate the appropriate getters in your bean with `@NotEmpty` - and you're done. I believe that's what OP meant by "agile". Note that I don't have anything against Spring Validator.
ChssPly76