This project is read-only.

CompareTo Validation Attribute

Jan 21, 2009 at 9:58 AM

I was code a signup form this morning with xVal and started to curse the gods that there was no DataValidation Attribute from the DataAnnotations namespace although there is one in the Castle Validation but it's not supported in your tests or in the javascript as far as I could see.

Any ideas on how I could either, roll my own extension to the MS DataAnnotations namespace and add support in xVal for Jquery or simply let me add some custom client side jquery to the xVal clientside output?

Jan 21, 2009 at 10:08 AM
Hi Mark

The current alpha release of xVal doesn't have any support for "compare" validators - this is a high priority to be included in the next release. That will likely happen a little while after ASP.NET MVC RC is released, so I can ensure xVal is compatible with it.

In the meantime, you could either dig into the xVal source code and add support yourself (following the same pattern as how other validators are implemented) or just augment your page with some custom JavaScript to attach extra jQuery Validators following the documentation at
Jan 21, 2009 at 11:32 AM
Looks like I will have to create a class level attribute in order to pass the whole object into the validation attribute then use reflection against a string array of properties I want to check are the same values. Not considering of course checking that the data types of all the properties I want to compare match. Then I will need to add support in xVal...

I could do with something like

[Compare(r => r.password==r.confirmpassword, ErrorMessage="Your passwords do not match")]
public class Registration
public string username {get; set;}
public string password {get; set;}
public string confirmpassword {get; set;}

I'll give it some more thought.

Thanks for the help though Steve.

Jan 21, 2009 at 11:53 AM

I guess reflection and strings is the best one can hope for currently.

I think I will just mash in some custom jquery validate code.

Jan 21, 2009 at 12:03 PM
Edited Jan 21, 2009 at 12:09 PM
Castle Validation does comparison validators a bit like this (pseudocode, since I can't remember its actual API right now):

public class Registration
public string username {get; set;}
public string password {get; set;}

        [Compare(CompareTo="password", Operator="Equals", ErrorMessage="Your passwords do not match")]
public string confirmpassword {get; set;}

I was planning to give xVal a new rule (or rules) that can model this, then add detection and handling of comparison validators across all supported server-side validation frameworks. There are a number of server-side validation frameworks that do comparison validators in approximately this way.

If you have time, then please feel free to have a shot at implementing this and submit a patch. Otherwise I'll do it as part of the next release.
Jan 21, 2009 at 1:55 PM
I have just taken a look at the Castle Validation library and yes they do indeed support different type of Comparison (greater, less, equalto, etc...). In my Googling I have also come across this project which are doing much the same sort of thing that you are although you will be locked into their Validation library and jquery.validate.

They have a comparison validation attribute which uses reflection to compare the property value with the value from another property. There is a lot of checking to be done in the comparison validation it seems which is why DataAnnotations probably doesn't contain a comparison attribute as it is relatively new.