Which validation library for ASP.NET MVC?
Asked Answered
A

4

11

I'm trying to decide what validation approach to take for a new ASP.NET MVC project. (And wow there are plenty of options!)

The project uses NHibernate, so the first thing I considered was the NHibernate Validator (Because of tight integration with NHibernate). However, as far as I can see there are only a couple of benefits to this tight integration:

1) DB Schemas generated by NHibernate will include details of validation (e.g. column lengths will be set to max value allowed in validation). (This is not really of interest to me though, as I generate schemas manually.)

2) NHibernate will throw an exception if you try to save data that doesn't meet the validation specs. (This seems fairly redundant to me, since the data presumably will already be validated by whatever mechanism you choose before saving anyway)

If there are more benefits to NHibernate Validator please let me know!

Other libraries which I've been reading a little about include:

  • MS DataAnnotations
  • Castle Validator
  • Something else?

I've also been thinking about using xVal to provide client side validation from the same set of rules. However, I hear that ASP.NET MVC v2 will include something similar to xVal (integration with jquery) out of the box? Will this new included functionality render some of the others redundant?

So, I'm basically asking for people's advice on which direction to take here. I don't want to implement a particular scheme, only to have to rip it out when another one becomes the dominant tech.

What has worked for you? Which option do you think has/will have the edge?

Thanks!

Amylase answered 12/10, 2009 at 10:33 Comment(0)
M
10

I have been using FluentValidation along with jQuery validation plugin and still cannot find a situation they cannot handle.

Malo answered 15/10, 2009 at 19:44 Comment(4)
This looks very interesting, thanks for posting. I'll take a deeper look later - first glance it seems it might be v good for business rule validation. Basic data validation may be a bit more verbose, but not a big deal. Do you know how well this setup handles internationalisation?Amylase
For i18n you could put all messages into a resources file and use them in the WithMessage FluentValidation method.Malo
I've accepted this as the answer, as it's a library I've not seen before. It looks good, and I'm looking into implimenting it now.Amylase
Darin, do you have any info you would share about how to integrate FluentValidation and jQuery Validation? As an example, I'd imagine it would be nice to use the same messages for rules that are validated on both the client and server sides.Foresheet
I
5

I like xVal.

You can implement very easily client and server validation with it. Also there is support for column (property) validation on entities that you would like to use.

Ilailaire answered 12/10, 2009 at 11:3 Comment(4)
Thanks. What underlying validation mechanism are you using? Castle? Data Annotations?Amylase
Not sure about Misha, but we use xVal + DataAnnotations. I built a T4 generator that emits our business objects and DAL, and it puts the appropriate attributes on the data members.Ninnette
DataAnnotations, but to be honest I've just chosen to work with DataAnnotations without investigation if some other mechanism would be better. So, I don't know why one would be better then another.Ilailaire
The thing that worries me about data annotation is that there currently is no runner provided by MS. The xVal project seems to supply one, but with caveats: [Quote from runner code comments] Runs each ValidationAttribute associated with a property on the supplied instance and returns an ErrorInfo relating to each validation failure. Caution: certain ValidationAttribute types claim to be valid even when they aren't - this runner would need to detect those special cases if you plan to rely on it. Fortunately, other validation runners...report all their errors correctly. [/Quote]Amylase
M
1

DataAnnotations implemented by buddy classes and JQuery client validation

Make sure you're using MVC Preview 2

Mam answered 15/10, 2009 at 19:42 Comment(1)
Yeah, that does look like it will be the default choice in v2 doesn't it. I'm wondering if it's just them going the MS way with Data Annotations though, rather than a real best practice.Amylase
F
1

You might be interested in this delegate approach. I was because i didn't like the xVal idea (the solution im currently going with) and the fact that it didn't seem to cater for complex validation cases that crossed multiple properties of the same or even different class structures.

Forbiddance answered 17/10, 2009 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.