Please see my newer article here.
Pages
Blogroll
-
RSS Links
My Tweets
- GONeale earned the Suspicious badge for Visual Studio Achievements: http://t.co/apqgBwgt via @ch9 #VSAchievements 2 weeks ago
- Seriously?! I never thought I would see this, but I guess I'm going to have to install.. :p http://t.co/bu93W9kN #VisualStudio #Achievements 2 weeks ago
- Calling all Aussies, have you checked out my new start-up? http://www.stayoftheday.com Would REALLY appreciate the support, share and earn credit! 3 weeks ago
- It's been a long time coming! I'm proud to announce my startup @StayOfTheDay pre-launch! Check it out, $500k giveaway http://t.co/nmh4rFp4 2012-01-11
- Woohoo! I just got my slice of the @StayOfTheDay $500K giveaway, no more full price accommodation for me! Got yours? http://t.co/nmh4rFp4 2012-01-11
- Wow! Database Schema and Data Compare has been in Visual Studio since 2008?? I never got THAT memo! http://t.co/TcWNZ3Tt #VisualStudio #.NET 2011-12-22
- More updates...
Advertisment
18 Comments
Folks may want to consider the Enterprise Library Validation Application Block as well.
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)
I didn’t see you mention xval. xval seems to have taken lots of these thoughts into consideration: http://www.codeplex.com/xval
great article,thx
This looks great, I was just about to write something similar myself. What version of Castle are you using?
I already found it, the dll was hidden in the web-directory
Awesome! Checking it out now! Thanks for that work.
Only the white on black in your blog drove me slowly insane after a while
I wonder, why don’t you use XVal?
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 =:)
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.
Thanks Trevor, I read Paco’s comment first but have now made a mention to this framework.
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
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.
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…
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’.
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…
@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/.
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
4 Trackbacks/Pingbacks
[...] 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, [...]
ASP.NET MVC Validation – The Definitive Guide (in my eyes) « {Programming} & Life…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
[...] ASP.NET MVC Validation – The Definitive Guide: Graham O’Neale has compiled the ultimate guide on ASP.NET MVC validation. [...]
[...] in Uncategorized Well major updates to my old article on ASP.NET MVC Validation [...]
Post a Comment