DataAnnotation & Sample ValidationRunner

Apr 16, 2009 at 4:52 PM

Take a look at this:

Looks like a good idea to provide a model binder for each attribute based validation framework.

Also after reading that thread about the serverside DataAnnotations attributes not validating correctly I took a look at the attribute code through reflector and although I am no expert but it looks like none of DataType attributes work correctly.

Apr 16, 2009 at 5:20 PM
Looks like a good idea to provide a model binder for each attribute based validation framework.

I'm not so sure. Architecturally, why would you want model binding have anything to do with validation? 

Typically I'm going for an approach to validation that encompasses business rules just as easily as simple property formatting rules. Business rules obviously belong in the model layer, not up in the HTTP-request-centric UI world of ASP.NET MVC model binders. So, typically I want my model layer to have the final say over whether something's valid, not a controller or its choice of model binder. The determination of validity might be a combination of DataAnnotations attributes and arbitrary C# code.

I appreciate that Brad's post is explaining that DataAnnotations exist and he cites a simple way of using it. That's cool, but I think we've already pushed beyond that and have a tidier arrangement. But what do you think? Am I missing some important point here?

> it looks like none of DataType attributes work correctly
I was a bit confused about that at first, too! I can only guess that the idea is that the DataAnnotations attributes are meant to convey information only, not behaviour. However, it's strange that they have implemented the validation behaviour for some attributes but not others. Not totally certain why that is. I'm hoping that someone will make a nice open source runner for the DataAnnotations attributes which fills in the gap here - don't really want to make xVal responsible for this.
Apr 17, 2009 at 8:14 AM
I emailed Brad about the DataType attributes and they are only UI hint attributes and not validator attributes even though they have a isValid method on them.
He said they are not to be used for Validation and instead use the regex attribute with an appropiate regex for your situation.

...Snip of Brad's email
The "DataType" of e-mail address would be mostuseful on the _output_ side, so that renderers could automatically createmailto: links when display e-mail address. Input validation of an e-mailaddress would probably use the Regex validator, if you can come up with a reasonableRegex to represent your view of a valid e-mail address.

In terms of the "sample" DataAnnotations validation runner that you posted with xVal what things need to be done to make it production ready, I have trawled the internet for validation runners for Nhibernate and DataAnnotations but xVal is the only result of worth.

I agree with not making xVal responsible for the server side validation of the model but it would be good if we could give people some steering on how to create a serverside validation runner.