Update: I've not forgotten about this everyone, apologies for the delay. My attempt to get approval to publish failed due to vacations (mine and others) so as soon as I can I'll try again.
My blog posts on doing conditional validation in MVC have always been popular, and I’m frequently surprised by where I find that code – customers often say to me “we found some guys blog with some code to do the validation we needed” and sure enough it usually turns out to be mine!
There are two problems though – firstly, my code was always intended to be a sample to get you started, and that meant I’d not written a comprehensive set of tests. Secondly, there are all sorts of validation cases that developers need that I hadn’t covered.
So I decided to refresh the code base, both with additional validators and with a pretty decent set of tests. The first Alpha (i.e. incomplete, probably buggy, etc, etc you have been warned ) release of this code is attached to this blog post.
There are four main topics included in the box;
Note that all of these pieces work both server-side and client-side (in the browser) if you use the JavaScript included in /Mvc.ValidationToolkit.Harness/Scripts/MvcValidationToolkit/*.js.
Also, note that they all have MSTest unit tests and automated WaTiN UI tests that exercise both the JavaScript and Server implementations of each feature. I’m positive it needs more tests (for example different data types).
I must emphasise that this release is a preview. So what next? Well I want to know a few things;
Please comment on this post if you’ve answers to those questions. But I also want to know where this should live and how it should move forwards. I would consider setting up a codeplex project if there were a few strong MVC & jQuery developers that wanted to add to and evolve the library. Or is this drop enough to get you started? Let me know your thoughts.
In the mean time, I may blog on using the attributes a little more if I get the chance.
To have a look at the code, download the attachment to this post and have a look in Readme.txt – the only thing you need to do is grab a couple of NuGet packages and everything should work. Note: if you’re running the WaTiN tests, run Visual Studio as elevated else you’ll just get timeouts.
I get the following errors when opening the Solution:
Mvc.ValidationToolkit\Mvc.ValidationToolkit.csproj : error : Unable to read the project file 'Mvc.ValidationToolkit.csproj'.
Mvc.ValidationToolkit\Mvc.ValidationToolkit.csproj(69,3): The imported project ".nuget\NuGet.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
Mvc.ValidationToolkit.Harness\Mvc.ValidationToolkit.Harness.csproj : error : Unable to read the project file 'Mvc.ValidationToolkit.Harness.csproj'.
Mvc.ValidationToolkit.Harness\Mvc.ValidationToolkit.Harness.csproj(220,3): The imported project ".nuget\NuGet.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
Mvc.ValidationToolkit.Tests\Mvc.ValidationToolkit.Tests.csproj : error : Unable to read the project file 'Mvc.ValidationToolkit.Tests.csproj'.
Mvc.ValidationToolkit.Tests\Mvc.ValidationToolkit.Tests.csproj(108,3): The imported project ".nuget\NuGet.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
Cheers Harry
@ Harry -
Oops sorry. Fixed now!
It was because I was using the NuGetPowerTools to auto-fetch NuGet dependencies that were not in my source control system. I actually found that to be a problem though as I often work offline on trains etc, and it would fail my build if it couldn't hit the NuGet servers. Looks like uninstalling the package didn't clean it properly.
All done now.
Simon
Very nice! How about publishing it somewhere (GitHub?) so that we can track changes more easily?
I have month and year drop-down for credit card expiration fields. Is there anyway to create validation attribute to validate future expiry date?
These look like good additions. Perhaps set this up at codeplex.com?
Better to add Enable and disable a field feature along with conditional logic.
I am interested in RequiredIfAttribute, one question is that does it support required if another attribute is not empty (string with any value)?
Thanks
In your GetClientValidationRules method on RequiredIfAttribute, you're using "yield return rule". You shouldn't do this. An outside caller could potentially recreate the rule multiple times.
Example:
var rules = GetClientValidationRules();
var rule1 = rules.First();
var rule2 = rules.First();
Object.ReferenceEquals(rule1, rule2) will return false.
Both times calling First() causes the dynamic enumerator created by a yield to start all over again.
You should, instead, return a new array with the rule in it.