Client side validation with DataAnnotations only validates numeric types - 1 possible solution

Dec 3, 2009 at 1:22 PM

I've been pulling my hair for the last couple of hours / days since between now and 1 month ago client side validation using xVal with DataAnnotations stopped working but server side validation worked fine.

The code generated by xVal in the html source looked like this (only validating my id field being of type int):

xVal.AttachValidator(null, {"Fields":[{"FieldName":"Id","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]}]}, {"ValidationSummary":{"ElementID":"validationSummary","HeaderMessage":null}})

For me this looked like either the jquery stuff or the xval dll was screwing something up ( of course you blame other people's code when something doesn't work :D ). When I had stepped through the jquery code using firebug I came to the conclusion that jquery was only working with the data passed on by the Html.ClientSideValidation in xVal.dll.

I included the source code in a new branch of my project and stepped into the relevant methods but still didn't see where the problem was and at this stage I had two solutions, my original solution with xval code imported and the booking demo solution with imported classes needed for validation from my project, one worked, the other didn't. It just seemed as it was only able to validate properties of type numeric?

When stepping through the code I found the place where things were going wrong. Line 32 in PropertyAttributeRuleProviderBase.cs
  where TAttribute is ValidationAttribute. When this code ran through one of my validated properties I quick-viewed the results of
to find out what was causing my error. This resulted in the following error:
The type 'System.ComponentModel.DataAnnotations.ValidationAttribute' exists in both
'System.ComponentModel.DataAnnotations.dll' and 'System.ComponentModel.DataAnnotations.dll'   

This means all validation attributes were ignored and only run serverside and the reason being at some point the System.ComponentModel.DataAnnotations.dll was referenced not from the framework but from a hard drive location (argh!). Some people might be referencing the dll this way and will possibly run into this error. Maybe this will save someone a couple of minutes debugging.