Skip to content

ASP.NET MVC Validation – The Definitive Guide (in my eyes)

Please see my newer article here.

VN:F [1.9.10_1130]
Rating: 2.9/5 (10 votes cast)
VN:F [1.9.10_1130]
Rating: +1 (from 3 votes)
ASP.NET MVC Validation - The Definitive Guide (in my eyes), 2.9 out of 5 based on 10 ratings
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

18 Comments

  1. Shawn

    Folks may want to consider the Enterprise Library Validation Application Block as well.

    VA:F [1.9.10_1130]
    Rating: 3.0/5 (1 vote cast)
    VA:F [1.9.10_1130]
    Rating: -3 (from 3 votes)
    Posted on 06-Feb-09 at 3:24 am | Permalink
  2. Just going through your post, sounds interesting.
    One note for string.format – if you would like to have a { in the format, you can user {{
    e.g.
    not string.Format(“function {0} {1}..”, fncName, “{“);
    but
    string.Format(“function {0}{{..”, fncName);
    (posting here although the code is probably from Ibrahim)

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: -1 (from 1 vote)
    Posted on 10-Feb-09 at 6:04 pm | Permalink
  3. I didn’t see you mention xval. xval seems to have taken lots of these thoughts into consideration: http://www.codeplex.com/xval

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 10-Feb-09 at 9:35 pm | Permalink
  4. great article,thx

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 10-Feb-09 at 11:03 pm | Permalink
  5. Paco

    This looks great, I was just about to write something similar myself. What version of Castle are you using?

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 10-Feb-09 at 11:28 pm | Permalink
  6. Paco

    I already found it, the dll was hidden in the web-directory

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 10-Feb-09 at 11:31 pm | Permalink
  7. Thomas

    Awesome! Checking it out now! Thanks for that work.

    Only the white on black in your blog drove me slowly insane after a while ;)

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 11-Feb-09 at 1:04 am | Permalink
  8. Paco

    I wonder, why don’t you use XVal?

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 11-Feb-09 at 1:39 am | Permalink
  9. Graham O'Neale

    lol thanks for your comments. I know, I’ve been thinking to change the white on black code snippets for a while now, but as I post more articles I’m tending to not think it’s so bad as you can easily identify code blocks. But I’m moving to dedicated hosting soon, so when I do I’ll try mucking around with it a bit more =:)

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 11-Feb-09 at 8:33 am | Permalink
  10. Graham O'Neale

    Thanks for the comments Paco. I actually hadn’t read up on that yet! but it does look good, very elegant framework with similar ideologies. For those who want more information check it here: http://blog.codeville.net/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/. Having said that, you may still need to incorporate my approach with “SOLUTION B” if you cannot make direct changes to your DAL model classes.

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 11-Feb-09 at 8:40 am | Permalink
  11. Graham O'Neale

    Thanks Trevor, I read Paco’s comment first but have now made a mention to this framework.

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 11-Feb-09 at 8:41 am | Permalink
  12. Thomas

    Hey Graham,

    I played with your code a bit and implemented a testscenario in my own mvc app. If I do not put the code bracket for the client side validation in, I will not see any validation. I don’t think this is intended? Can you perhaps check that back? I’m still searching for a convenient way to do the validation for our new project and your approach looks very reasonable so far, so I would greatly appreciate your review ;)

    Btw, Castle seems to have updates their dll. With the downloaded version your solution will not compile. Only in case you want to update ;)

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 11-Feb-09 at 1:19 pm | Permalink
  13. Graham O'Neale

    Hi Thomas, Yes you do need the Html.ClientSideValidation() function for the validation to render, this is as intended and as per Emad’s example. As this gives you flexibility also to exclude validation from some pages if necessary.
    Thanks for the heads up regarding Castle, I will attempt to update the example when I get time, I will be testing out the new xVal library shortly, so I suggest you take a look at that and compare both implementations =) Take care.

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 14-Feb-09 at 6:54 pm | Permalink
  14. rekna

    I don’t see how solution B works on the server side? How can you tell Castle Validation to get the attributes from the metadataclass ?

    When I look at the example project, I only see a reference to MetaDataTypeAttribute inside ValidationScritpsHelper…

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 03-Mar-09 at 6:35 pm | Permalink
  15. Graham O'Neale

    Rekna,
    Castle Validation looks at the Metadata class via the foreach() statement on line 36 of my example of ValidationScripts.cs. We loop through the attributes searching for any matching the type ‘MetadataTypeAttribute’.

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 03-Mar-09 at 9:50 pm | Permalink
  16. rekna

    ValidationScripts is only client-side validation, or am I wrong ? I was wondering how Castle does server side validation with MetadataType…

    The ValidatorRunner doesn’t seem to handle the MetadataType…

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 03-Mar-09 at 10:02 pm | Permalink
  17. Graham O'Neale

    @rekna, sorry dude just downloaded the code sample and tested it, you’re right. Solution B works for client-side using the metadata type attribute but not for server-side, I must have got sidetracked and thought I did it when I didn’t. And it looks like it’s not an easy task to do as the castle validator itself will have to be edited (not really an option) or overloaded.

    Don’t use my solution now unless I get a chance to get it working as it should be.
    I suggest you check out xVal, from what I understand it is meant to have support for metadataType Steve claims but I am yet to test. His approach is probably better as it’s a more sophisticated framework http://blog.codeville.net/2009/02/27/xval-08-beta-now-released/.

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: +1 (from 1 vote)
    Posted on 04-Mar-09 at 8:51 am | Permalink
  18. Hi Graham,

    Thanks for the great article! I recently spent two days on figuring out how to manage server- and client-side validation in a generic way and found a way to use xVal, DataAnnotationsModelbinder and jquery.validate so that I can implement any validation rule (even remote client-side validation) with C# code only. If you’d like to have a look, you can find the article at

    http://devermind.com/aspnet-mvc/asp-net-mvc-tip-3-how-to-cover-all-your-client-side-form-validation-needs-without-writing-any-javascript-part1

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
    Posted on 24-Jun-09 at 12:19 am | Permalink

4 Trackbacks/Pingbacks

  1. ASP.NET MVC Archived Blog Posts, Page 1 on 22-Jan-09 at 1:08 pm

    [...] to VoteASP.NET MVC Validation – The Definitive Guide (in my eyes) (1/18/2009)Sunday, January 18, 2009 from Graham O’NealeThe thing I attempt to pride myself on with this blog, [...]

  2. DotNetShoutout on 05-Feb-09 at 9:53 am

    ASP.NET MVC Validation – The Definitive Guide (in my eyes) « {Programming} & Life…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

    VA:F [1.9.10_1130]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.10_1130]
    Rating: 0 (from 0 votes)
  3. [...] ASP.NET MVC Validation – The Definitive Guide: Graham O’Neale has compiled the ultimate guide on ASP.NET MVC validation. [...]

  4. [...] in Uncategorized Well major updates to my old article on ASP.NET MVC Validation [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...