Site Meter

xVal – a validation framework for ASP.NET MVC

Update: The version of xVal presented in this post (and in its downloadable demo project) has been superseded by a newer version with extra features. This post is still a good introduction to the concept, but to download the latest version of xVal please go to Thanks!

logo100pxI’ve written about validation in ASP.NET MVC more than once already. So have others (Stephen Walther, Emad Ibrahim). It’s an ongoing subject of discussion because the MVC Framework comes with great support for server-side validation (though it isn’t obvious if you come from a WebForms background), but at least as of version 1.0, it won’t ship with any built-in support for client-side validation.

I managed to get a few spare days this last week, so I decided to try and formalize lots of these ideas into something solid that you can actually use in real production code – something more than just another throwaway blog post. So here it is: xVal, an open-source project hosted on CodePlex.

What does it do?

xVal lets you link up your choice of server-side validation mechanism with your choice of client-side validation library. It guides you to fit them both into ASP.NET MVC conventions, so everything plays nicely with model binding and errors registered in ModelState.


The design goals are as follows:

  • Fit in with ASP.NET MVC conventions
  • Promotes a clear validation pattern that works whether or not the visitor has JavaScript enabled (graceful degradation)
  • Lets you plug in your choice of server-side validation framework or custom code. The current version includes rules providers for .NET 3.5’s built-in DataAnnotations attributes as well as Castle Validator attributes, but you can also write your own IRulesProvider to attach rules programmatically or using any other .NET validation framework.
  • Lets you plug in your choice of client-side validation library. The current version includes plugins for jQuery Validation as well as the native ASP.NET validation code (as used in WebForms), but you can use the standard JSON-based rules description format to write plugins for any other client-side validation library.
  • Supports internationalization, so you can vary the display of error messages by the current thread UI culture.
  • To be a high-quality product. xVal is built using TDD and comes with a solid set of unit tests. It uses xUnit and Moq for testing the server-side code, and Selenium RC for testing the client-side code.

The current version on Codeplex – version 0.5 – works with ASP.NET MVC Beta. It’s mostly solid and usable, though there are a few rough spots (e.g., a couple of inconsistencies to do with date formatting) that I’ll iron out soon. I also have plans to improve the internationalization somewhat to let you get error messages in other languages without having to specify them explicitly. Also, I’ll be adding support for DataAnnotations’ notion of “buddy classes” so that it works much better with the LINQ to SQL designer’s generated entity classes.

Quick tutorial

I don’t have time to document everything in xVal just yet. The basic usage pattern is supposed to be straightforward so hopefully the following tutorial won’t seem too clever or weird.

Let’s say you’ve got a model class defined as follows. Notice that we’re using attributes such as [Required], so you need to reference the System.ComponentModel.DataAnnotations.dll assembly:

public class Booking
    [Required] [StringLength(15)]
    public string ClientName { get; set; }
    [Range(1, 20)]
    public int NumberOfGuests { get; set; }
    [Required] [DataType(DataType.Date)]
    public DateTime ArrivalDate { get; set; }

You might add code to an ASP.NET MVC controller to create instances of this class:

public class HomeController : Controller
    public ViewResult CreateBooking()
        return View();
    public ActionResult CreateBooking(Booking booking)
        return RedirectToAction("Completed");
    public ViewResult Completed()
        return View(); // Displays "Thanks for booking"

This controller references some code in the domain tier that actually places bookings:

public static class BookingManager
    public static void PlaceBooking(Booking booking)
        // Todo: save to database or whatever

Of course, you’d also need a view for CreateBooking, containing the following markup:

<h1>Place a booking</h1>
<% using(Html.BeginForm()) { %>
        Your name: <%= Html.TextBox("booking.ClientName") %>
        <%= Html.ValidationMessage("booking.ClientName") %>
        Number of guests: <%= Html.TextBox("booking.NumberOfGuests")%>
        <%= Html.ValidationMessage("booking.NumberOfGuests")%>
        Expected arrival date: <%= Html.TextBox("booking.ArrivalDate")%>
        <%= Html.ValidationMessage("booking.ArrivalDate")%>
    <input type="submit" />
<% } %>

This is all fine, but so far, no validation is enforced.

Enforcing server-side validation

As I’ve discussed before, validation rules should go in your domain model, because the role of your model is to represent the workings of your business. It ensures that the rules will be enforced consistently, regardless of whether the UI coder remembers them or not.

At this point, reference xVal in both your domain model project and in your ASP.NET MVC project.

Let’s update BookingManager.PlaceBooking():

public static class BookingManager
    public static void PlaceBooking(Booking booking)
        var errors = DataAnnotationsValidationRunner.GetErrors(booking);
        if (errors.Any())
            throw new RulesException(errors);
        // Business rule: Can't place bookings on Sundays
        if(booking.ArrivalDate.DayOfWeek == DayOfWeek.Sunday)
            throw new RulesException("ArrivalDate", "Bookings are not permitted on Sundays", booking);
        // Todo: save to database or whatever

Now, the model tier enforces its own validity by refusing to place bookings that don’t meet all validation and business rules. It throws a special type of exception, RulesException (defined in the xVal assembly) that passes structured error information back to the caller.

Aside: The best way of thinking about business rules, in my opinion, is that you’re validating an operation (i.e., a unit of work). For example, “documents can only be published when they have been approved by someone in the Moderator role” – this is an act of validation on the operation of publishing.

The DataAnnotations attributes don’t come with a validation runner. Here’s a very simple (and slightly naive one) referenced by the previous code block:

internal static class DataAnnotationsValidationRunner
    public static IEnumerable<errorInfo> GetErrors(object instance)
        return from prop in TypeDescriptor.GetProperties(instance).Cast<propertyDescriptor>()
               from attribute in prop.Attributes.OfType<validationAttribute>()
               where !attribute.IsValid(prop.GetValue(instance))
               select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), instance);

Finally, let’s fit in with ASP.NET MVC conventions by transferring any detected errors to ModelState:

public ActionResult CreateBooking(Booking booking)
    try {
    catch(RulesException ex) {
        ex.AddModelStateErrors(ModelState, "booking");
    return ModelState.IsValid ? RedirectToAction("Completed")
                              : (ActionResult) View();

Now, because we already have Html.ValidationMessage() helpers in the view, our error messages will appear:


This is the essence of server-side validation. You validate requested operations, both in terms of model properties and arbitrary business rules expressed in C# code, and if anything is bad, you put the error information into ModelState and then re-render the view.

So far, we’ve only used xVal as a way of transferring error information from the model tier to the UI tier. The clever bit happens when we want to add client-side validation.

Applying client-side validation

xVal comes with support for native ASP.NET client-side validation and jQuery Validation. Let’s use jQuery Validation. Add jquery.validate.js (which you can download here) and xVal.jquery.validate.js (included in xVal) to your /Scripts folder, then add references in your master page:

    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.2.6.js")%>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery.validate.js")%>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/Scripts/xVal.jquery.validate.js")%>"></script>

Also, import xVal’s HTML helpers by adding the following line to your web.config’s <namespaces> node:

        <!-- leave rest as-is -->
        <add namespace="xVal.Html"/>

Now, all you have to do is tell xVal that you want to apply client-side validation to the controls with IDs prefixed by “booking.”. Add the following line to your view:

<%= Html.ClientSideValidation<booking>("booking") %>

That’s it! jQuery Validation will now receive and enforce the rules defined by attributes.


Of course, if someone has JavaScript turned off, they’ll still be subject to the server-side validation enforced in the model tier. Also, the business rule about not allowing bookings on Sundays will still be enforced on the server either way.

Changing client-side validation libraries

If you decide you don’t like jQuery Validation, it’s trivial to switch to a different client-side library as long as you have an xVal plugin for it. For example, to switch to native ASP.NET validation, just remove the reference to xVal.jquery.validate.js (and the other jQuery references if you want), and reference the following instead:

<script type="text/javascript" src="<%= Page.ClientScript.GetWebResourceUrl(typeof(System.Web.UI.Page), "WebForms.js") %>"></script>
<script type="text/javascript" src="<%= Page.ClientScript.GetWebResourceUrl(typeof(System.Web.UI.Page), "WebUIValidation.js") %>"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/xVal.AspNetNative.js")%>"></script>

Job done! You’re now using the native ASP.NET client-side validation libraries (as used by WebForms) and don’t need to use jQuery.

Download the finished demo project

What next

Over the next few weeks, I’ll try to find time to document the following features:

  • How to add custom validation logic that works both on the server and on the client
  • How to supply validation rules programatically or explicitly (i.e., if attributes on properties don’t meet your needs, or if you don’t like model-based validation)
  • How to internationalize your validation messages
  • How to write an IRulesProvider or a plugin for a client-side validation library
  • How to control the placement of validation messages
  • How to reuse a single rules configuration across multiple groups of form controls (e.g., when editing a list)

Comments and questions are appreciated, as well as offers to contribute to the xVal project! (For example, you could write a provider or plugin for a different server-side or client-side validation toolkit.)

In case it wasn’t obvious, xVal is hosted on CodePlex at It’s open source, under the MS-PL license.

139 Responses to xVal – a validation framework for ASP.NET MVC

  1. Very nicely done and very similar to the way I did it but you put it together nicely and you have a nicer name :)

    I love your blog. Keep up the good work.


  2. Rodrigo

    This is a very elegant solution, thanks a lot for the work.

  3. Awesome! I’ll try it right now:-)

  4. Pingback: Dew Drop - January 10, 2009 | Alvin Ashcraft's Morning Dew

  5. Mike

    Excellent, thanks for the work you did! I will try it out next week @ work and leave feedback on Codeplex.

    One thing that is smart is to allow different options on both sides through plugins or implementing interfaces. imho Microsoft data annotations are inadequate, because they don’t have a runner. But then the good thing about it is that it’s part of the framework. So I hope that there will be a runner from Microsoft or a good open source runner.

  6. VERY cool. Nicely done, congrats!

  7. Johan

    Very nicely done indeed!

  8. Andy

    Just read an article by Kirk Evans using Windows Workflow Foundation rules for entity validation ( That would be a fascinating “enterpise-y” Rules Provider to have in your framework.

  9. Pingback: Shared Tutorials » Blog Archive » xVal - a validation framework for ASP.NET MVC

  10. What I was looking for!!!!! Thanks

  11. Andy Hitchman

    Very cool. I’d like to suggest a small enhancement:

    How about the RulesException returning a collection of server-side errors?

    This would allow all server-side validation to be performed in one pass and prevent the slow reveal of business rule failures (fix the can’t book on Sundays error, submit and then get another error, fix that, submit, another bl**dy error!).

    In any case, will be trying it out on our upcoming PoC.

  12. Great framework! Is the current interface stable yet? I.e. will you be changing a lot of classnames / method names and so on? I’ld really like to add this into the book I’m currently writing.

  13. Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #262

  14. Pingback: Interesting Finds: 2009.01.12 - gOODiDEA.NET

  15. Steve

    Thanks everyone for your kind comments.

    @Andy: Interesting – I’ll check that out and may use that as an example when writing up “How to implement your own IRulesProvider”

    @Andy Hitchman: RulesException already does return a collection of ErrorInfo objects. But you’re right, I could improve the example code in this blog post by adding the “Sundays” rule to the existing collection of errors (i.e., the variable called “errors” in PlaceBooking()), so it would be displayed at the same time as any other errors.

    @Maarten: Thanks! Whether or not the API changes will depend on what feedback and suggestions I get. Once ASP.NET MVC Release Candidate becomes available, it should be possible for me to produce an updated version of xVal with an API that should be pretty stable for the future.

  16. Mike

    What about the IDataErrorInfo interface to get errors straight from the model object (could still use the same runner, but that way there is no need for your ErrorInfo class, right?

  17. Cyril Bioley

    Very cool initiative ! I’ll dive in it now.

  18. Great stuff, and just to say I think having the first two items in your “What Next” list would definitely be very useful in working out how to get going with it.

    Anyway we’ll definitely be evaluating this for our project and nice work.

  19. The best validation framework for ASP.NET MVC. Congrats for your great work.

  20. Steve,

    This is fantastic. A huge thank you for sharing this. Having attempted to create a model-based validation framework for MVC and getting in way over my head, I’ve been patiently waiting and hoping that someone with more insight and experience (and time!) would lead the way. So when your post came into my feed reader last night, I shouted out loud. This looks like a big step forward.

    I’ll be incorporating this into my work soon and let you know if I hit any rough spots. But I’m very much looking forward to your updates on this project, and wish the best for xVal’s success in the community.

    God bless,

  21. This is great. I especially like that you’ve decoupled it from the actual rule providers, this way you extend to a larger audience and avoid the religious wars.

    I fully expect a client side validation implementation after RTM sometime, but until then, this will be helpful.

  22. Mike

    Regarding your “Thoughts on validation in ASP.NET MVC applications”, how would you fit something like a unique constraint into this? Or something like a compare validator?

  23. Steve

    @Mike (post #16): That’s an interesting point. You could certainly tweak your server-side validation runner to take IDataErrorInfo into account. However, I don’t think it’s a suitable replacement for the ErrorInfo class because IDataErrorInfo doesn’t give you any means to discover the full set of error reports (unless you already know all the possible corresponding property names) and it doesn’t tell you which object (e.g., out of a collection of model objects) the errors relate to. I’ll review this when I get ASP.NET MVC RC because Microsoft might have changed the error reporting conventions.

    @Mike (post #22): I’d implement a unique constraint in the model tier (perhaps by catching any SqlException thrown when saving a record to the database, or perhaps by checking for a clash inside a critical section with a write lock). Most of the time I don’t think it would be worthwhile to try implementing this as a client-side rule too. A client-side uniqueness check would never be totally robust anyway because a clashing value could be submitted before the user submits the form.

    I haven’t yet given xVal any support for comparison-based validation rules. However, this is definitely coming soon in an upcoming version so that I can fully support all DataAnnotations attributes and Castle Validator attributes.

  24. Mike

    By the way, I don’t think the DataType attribute is supposed to validate, the docs state that:

    “This information is then accessed by the field templates to modify how the data field is processed.”

    Looking in Reflector, the IsValid method for DataTypeAttribute always returns true.

    Yeah, I was a bit disappointed too, back to the RegularExpressionAttribute then.

  25. Steve


    > By the way, I don’t think the DataType attribute is supposed to validate

    I was thinking of providing a server-side DataAnnotations runner that has somewhat more useful behaviour for DataTypeAttribute instances. For example, [DataType(DataType.Email)] would actually check for a valid email address. Do you think that would be useful, or do you think it violates the understood semantics for a DataTypeAttribute instance?

  26. This is really looking useful. Thanks!

    I’ve had a quick spike to try it out, against the System.ComponentModel.DataAnnotations attributes. I’ve subclassed RegularExpressionAttribute there, like [1].

    How I laughed at the default error message that comes out [2] – I thought I’d share before I post myself to the dailywtf ;-)

    public class EmailAttribute : RegularExpressionAttribute {
    private static readonly String emailRule = @”^([a-zA-Z0-9_\-\.\']+)@((\[[0-9]{1,3}” +
    @”\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\” +
    public EmailAttribute() : base(emailRule) { }


    Email [jhskdhf]
    The field must match the regular expression ‘^([a-zA-Z0-9_\-\.\']+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$’.

  27. Steve

    @Peter: What’s wrong with that error message? It’s clear and precise :)

    Yeah, that is how System.ComponentModel.DataAnnotations.RegularExpressionAttribute reports errors by default. Of course, it’s normal to specify your own custom ErrorMessage value for such a rule. Or in this case, to use [DataType(DataType.EmailAddress)] instead.

  28. Mark

    Just found out that the Html.ClientSideValidation(“myprefix”) renders a script block that needs to be run after the page has loaded. I was putting this in the head element before the form had loaded. I had to copy your HTML helpers into my project and amend to put a $(document).ready(function(){}); wrapper around the javascript to make it work from the head. Any chance of an update with a more elegant solution?

  29. Steve

    @Mark – that’s a really good point and I’ll try to solve that in the next version. I don’t want the core product to rely on jQuery so I’ll need to figure out how to add a cross-browser on-DOM-ready handler without jQuery.

    If you discover any other issues or want to suggest other enhancements, please use the issue tracker on CodePlex ( That will help me keep organized. Cheers!

  30. mike

    I personally don’t think you should use the datatype annotation in a different way. It’s more of a hint, although I don’t understand why it inherits from validation attribute. I just use RegularExpression to validate an email address or url.

    I wrote a runner for data annotations as well, I found them to be lacking (no minimum length for strings, there’s no built in runner, datatype is just confusing). I wonder what Microsoft will do with it. ASP.NET MVC 1.0 will not have anything for validation except the basic modelstate infrastructure, but the MVC DynamicData stuff must include at least a runner and client side validation. But I don’t know if that will be suitable for our own use.

    Until then, this project is really needed, so thanks again!

  31. Pingback: Good frameworks for IOC and Validation | Just Programmer

  32. Pingback: links for 2009-01-15 « MyNotePad

  33. Pingback: Shared Tutorials » Blog Archive » xVal - a validation framework for ASP.NET MVC « Steve Sanderson’s blog

  34. very nice post…was(and still is) looking for something like this…Thanks :)

  35. Alex

    Looks great! Can this integrate with Validation Aspects (

  36. i bookmarked this article….thank you

  37. Steve

    @Alex – looking at the source code for ValidationAspects, it certainly looks as though you could develop an xVal IRulesProvider for it. You would need to scan for attributes inheriting from ValidatorAttribute, then inspect their Factory property to see whether it was a MaximumLength or a MatchesRegex etc… Pretty much all the ValidationAspects rules correspond to existing xVal rules.

    I’d be interested to hear how you get on if you try doing this yourself. The next IRulesProvider that I personally was going to write would be for NHibernate Validator, though I don’t actually know if many people use that. Maybe I should do some sort of poll to find out what server-side frameworks people most want support for.

  38. @Alex – there is an Emit feature introduced in the latest version of ValidationAspects that should simplify integration. I haven’t documented it yet but usage is demonstrated in the unit tests.
    I’d be interested to hear how you get on also.

  39. Ray

    Hi Steve, I have a quick question about xVal, does it work in medium trust? Thank you so much, -Ray.

  40. Steve

    @Ray – xVal certainly should work fine in medium trust environments, with the exception of a certain part of the Castle Validation provider which depends on private reflection to overcome a troublesome limitation in the Castle Validation API. If you’re using the System.Web.ComponentModel.DataAnnotations provider (i.e., the default, and not Castle Validation), then there is no such issue so it should be no trouble.

  41. Larry Larrigan

    Does xVal work with ASP.NET MVC RC 1.0 ?

  42. Bart

    Hi Steve,
    xVal looks promising, however I will wait until the NHibernate Validator is supported.
    Keep up the good work!

  43. Nicolas

    +1 for NHibernate validator

  44. Ant

    @Larry & RC 1.0
    I did have a problem using jquery validation, because the generated Ids have any ‘.’ replaced with ‘_’ so that its easier to use jquery selectors.
    var elemId = (elementPrefix ? elementPrefix + “.” : “”) + fieldName;
    var elemId = (elementPrefix ? elementPrefix + “_” : “”) + fieldName;
    in xval.jquery.validate.js

  45. Juan

    This is a great framework, many thanks for sharing

    just wondering if its possible to list the errors in a validation summary instead? I have tried:
    It lists the errors, but because the messages dont include field names its obviously just a list of meaningless messages so i guess my qustions is how can i get this to work is there a way to implement a validation summary or a way to customise the error message so the validation summary makes a bit more sense using the code above

    keep up the good work

  46. Juan

    oops the code of course was stripped out for my above is

  47. Todd Smith

    We can also take advantage of the new Metadata functionality in DynamicData which will allow us to separate our validation rules from our domain model.

    For example:

    namespace DomainModel
    [MetadataType (typeof (BookingMetadata))]
    public partial class Booking
    public string ClientName { get; set; }
    public int NumberOfGuests { get; set; }
    public DateTime ArrivalDate { get; set; }

    public class BookingMetadata
    [Required] [StringLength(15)]
    public object ClientName { get; set; }

    [Range(1, 20)]
    public object NumberOfGuests { get; set; }

    [Required] [DataType(DataType.Date)]
    public object ArrivalDate { get; set; }

    and one simple change to DataAnnotationsValidationRunner:

    public static IEnumerable GetErrors(object instance)
    TypeDescriptor.AddProvider (new AssociatedMetadataTypeTypeDescriptionProvider (instance.GetType ()), instance);
    return from prop in TypeDescriptor.GetProperties(instance).Cast()
    from attribute in prop.Attributes.OfType()
    where !attribute.IsValid(prop.GetValue(instance))
    select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), instance);

  48. Rodrigo Leote

    +1 NHibernate Validator

  49. Hi Steve! I’m a real late comer to this article, and frankly quite ashamed of it! =:) Really clean implementation and love the flexibility you give between choice of frameworks (client and server). I wrote a similar article a few days after yours (and if I read yours I probably wouldn’t have bothered! lol) leveraging Emad’s approach using Castle Validator and attributes, however I suffered problems incorporating validation attributes on my domain model objects when using Linq2Sql. So if you have troubles in that regard, hopefully I can still put forward help there with my solution to it :)

  50. Tony C

    I am a new comer to this article too. But I am having problem with the download demo project. I don’t seem to see any clientside validation done/working in Firefox or Safari ie. http://localhost:54146/Home/CreateBooking.

    I don’t have a production IIS yet, I am copying the link off IE direct to Firefox / Safari. Both browsers fail to repsonse with Clientside code, but Serverside works great. I am confused.

  51. Steve

    @Rodrigo – it’s coming! Not sure when exactly as I’m busy working as fast as I can on my ASP.NET MVC book. Hopefully within a couple of weeks.

    @Graham – thanks for the link. When you say you suffered problems with other validation approaches when adding attributes to LINQ to SQL classes, are you referring to the need for [MetadataType]? If so, xVal already supports this:

    @Tony – the downloadable demo project in this post is written for ASP.NET MVC Beta. I have updated xVal to work with ASP.NET MVC Release Candidate, but the change is just in the source code for now (downloadable from I am aiming to do a further proper release soon, at which point I’ll update this post. In the meantime, try using the source code version.

  52. Robert

    Thanks for great work.
    I have a question. If one form collects data from 2 models, how can I validate them?

  53. Steve

    @Robert – In exactly the same way. Just add another Html.ClientSideValidation() helper.

  54. Thanks Steve, I do mean the MetadataType! Cheers!

  55. Chad

    Thanks Steve, this has been an amazing help.

    One question. How can I attach validators to elements that don’t yet exist?

    For example, in my app at the moment, a user is presented with some data, they click a button, and with JS I convert these to input fields. I want to be able to validate this data before submitting.

    Any thoughts?

  56. Chad

    Just a follow up to my previous comment, here is the solution I’m working on:

    I added another overload for the ClientSideValidation helper to specify a bool for if it should output script tags.

    function configEditValidation() {
    <%= Html.ClientSideValidation(“Prefix”, false) %>

    Then I can wrap it in a JS function, “configEditValidation” and just call this method when I flip into edit mode after the form elements are added to the dom.

    Would still love some input / guidance as to other ways of accomplishing this.

  57. Steve

    @Chad – that sounds like a good solution. You might like to check out the existing Html.ClientSideValidationRules() helper, because this already does more or less what you want – it produces the ruleset in JSON format, so you can then call xVal.AttachValidator(“myElementPrefix”, ruleset) to attach validation manually.

    One of the things on my to-do list is documenting how to use xVal when editing a list (e.g., rows in an editable grid). This will use a similar technique to what you’ve just described. Hopefully that will make things clearer. For now though I’m using the little spare time I have for this to prepare the next xVal release which adds a whole bunch of valuable extra features.

  58. Richard

    Hello, Steve, I’m very much a beginner to ASP.NET MVC and a beginner programmer in general. I had a question regarding the internal static class ValidationRunner. Where exactly do I place it? Am I supposed to, somehow, add it to the DataAnnotations.dll aseembly, or create my own external class that implements it. Thanks.

  59. Steve

    @Richard – you can put the validation runner class anywhere in your solution. If you have a separate class library project for your domain model (or business logic, or whatever you call it) then it makes sense for it to go in there.

    It isn’t possible – unless you’re Microsoft – to add extra classes to System.ComponentModel.DataAnnotations.dll

  60. Richard

    Thanks Stev, I went ahead and implemented it in a separate class file exactly as you show. But I get the following 2 errors:

    -The type or namespace name ‘ErrorInfo’ could not be found (are you missing a using directive or an assembly reference?)

    -The type or namespace name ‘IEnumerable’ could not be found (are you missing a using directive or an assembly reference?)

    I’m using the assembly reference to System.Web for ErrorInfo. And if I try to implement the IEnumerable interaface by using System.Collections, I get the following error:

    -The non-generic type ‘System.Collections.IEnumerable’ cannot be used with type arguments

    Sorry for the long message, thanks.

  61. Richard

    Was able to get the genereic issue fixed, didn’t realize it had to be using System.Collections.Generic, that I had to use. I’m trying to see if there’s a similar error for the ErrorInfo message. I have a reference to Systems.Web in my project and I have a “using” directive in the class file, so I’m not sure what else might be missing.

  62. Richard

    Never mind, I wasn’t using the xVal directive.

  63. Stunningly great work!

    A small question though, I don’t really see how you can enforce comparison validation in the client side… Like having too textboxes with numbers and enforcing one to be greater than the other?

  64. Richard

    Hello Steve, it’s Richard. I’ve gotten much more familiar with the environment and was able to successfully implement xVal validation on one field. A question though, how to you get multiple field names to display errors using only server-side validation as you show in your example above. Currently, my implementation will only catch the first error and throw that, but it will skip the rest of the errors. Helps appreciated. My complements on your work.

  65. bill

    can it works on linq?
    like the FluentValidation on codeplex

  66. Steve

    @bill – if you mean “Can this work with LINQ-to-SQL-designer-generated classes?” then yes, it can. You’ll need to put your rules in some other “buddy” class using the [MetaDataType] attribute – see

  67. chief7

    In this example you have a datatype validation attribute ([DataType(DataType.Date)]). How do you make sure xVal is handling this validation and not the model binder? I am having an issue where the model binder is catching a bad date value setting the error in ModelState and sets the date property in my model to null so xVal never gets a chance to validate the actual value.

  68. Tim Hardy

    How do DataAnnotations or xVal handle the concept of different requirements based on the scenario taking place (e.g. Create versus Update)? I ask because my first attempt to use straight DataAnnotations resulted in me getting a ModelState.IsValid = false with a message of “ID is Required”. The problem is, I was about to a Create the entity in question, and it’s obvious that the Primary Key is not required when you are doing a Create. It is required when doing an Update. How can I mark my properties to handle these different scenarios using DataAnnotations?

    Also, I didn’t even mark my ID property as being required, so I’m curious as to how DataAnnotations figured out that it was required. I’m using LinqToSql, so I figure it’s interpreting one of the LinqToSql attributes, but I’d like to know exactly which one. If you know or could shoot me a link, I’d greatly appreciate it.

    I’m about to dive into xVal because it looks great.

  69. Steve

    @Tim – great question, one that others may be asking too. To make sure others can find and respond, I’ve moved this discussion to

  70. Steve

    @chief7 – the model binder will register errors about unparseable strings during model binding. Bear in mind that if the string is unparseable, there’s no way it could be assigned to a .NET DateTime property anyway, so setting the date property to default(DateTime) (which you call null) is the only sensible thing that could happen by the time the request comes in.

    xVal gives you client-side validation so date formatting errors will usually be caught and blocked on the client, preventing this whole problem from happening.

  71. Steve,

    Fantastic library. One item – is there a validation summary option for the client side script?

  72. Pingback: ASP.NET xVal Library with DataAnnotations at { null != Steve }

  73. Steve

    @Steve Gentile – so far xVal doesn’t create dynamic client-side validation summaries (because it’s a wrapper around whatever other client-side validation framework you’re using, and neither jQuery Validation nor ASP.NET native validation do validation summaries).

    However, it’s such a commonly requested feature that I’m thinking of ways to do it, for example by making a jQuery Validation add-on that does it, and then supporting that in xVal. Not sure how quickly I’m going to be able to do it (being generally too busy at present) so if you fancy having a go at implementing it like that, I’d be delighted to accept a patch.

  74. Schalk

    Seeing as an Unique Validator is pretty much required for most projects, I thought of a possible solution:

    public UniqueValidator : ValidationAttribute
    public UniqueValidator(Type repositoryType, string methodName)
    protected override bool Valid(object objToValidate)
    //use reflection to instantiate the repository
    //and call the method with required signature:
    // public bool (string compareString) {}

    1. Instantiate the repository via reflection.
    2. Wrap in in a try/catch and invoke the specified method with the value.
    3. Return bool indicating if valid.

    I’m not entirely sure how the reflection works with xVal to generate the , but it would be possible for this to automatically add the required script (only once if an Unique validation attribute is present) with added parameters such as the method to call (Json/Ajax) server side.

    Btw instead of doing as in the example to use the ValidationRunner and then do extra validation, just create a new class that inherits from ValidationAttribute and override the Valid method and add it to the metadata of the object to validate (same as required).

    Another tip is to use nested classes, example:
    public class MyValidation <- “wrapper”
    public class Entity
    public class NameValidator : ValidationAttribute
    //override the Valid method

    Then you can simply add a validation attribute by specifying [Entity.NameValidator].

    I’ll probably implement this sometime, thanks for the great framework!

  75. Dave

    So every time you have to change your model you’re going to re-implementing all property attributes? How is this going to work with the entity framework when the model gets overwritten?

  76. Pingback: June 7th Links: ASP.NET, AJAX, ASP.NET MVC, Visual Studio - ScottGu's Blog

  77. Pingback: 6月7日链接篇:ASP.NET, AJAX, ASP.NET MVC, Visual Studio-Scott Guthrie 博客中文版

  78. rick

    I’m not sure I like the idea of centralising the domain logic in my model… certainly not if I’m using schema driven development… I’d have to generate my objects and then add the validation logic using code dom. eugh.

    IMO validation should execute in your managers/business layer and also be implemented there … another reason – the validation logic applied to the model may be process specific!


  79. Steve

    @Rick – all that codegen nonsense is exactly the problem with “schema driven development” and why I avoid it.

    As for validation logic being process-specific – that scenario is handled trivially if both your rules and processes are owned by your model layer. For details, see

  80. Pingback: ASP.NET MVC Tip #3: How to cover ALL your client-side form validation needs without writing any JavaScript |

  81. Pingback: ASP.NET MVC Validation Refresh: Best Techniques & Frameworks | {Programming} & Life

  82. blu

    This really is impressive, thank you. I setup xVal with Adam Schroeder’s IDataErrorInfo implementation and this all works perfectly.

  83. Aziz Saidahmedov

    Beautiful indeed. THank you for so much working!!!. I like your blog

  84. mmtache

    the demo doesn’t work for me.. client-side validation doesn’t work.. only server-side validation works.

  85. Steve

    @mmtache – it’s because this demo is pretty old and is written for ASP.NET MVC Beta. I guess you’re using the RTM version of ASP.NET MVC which isn’t compatible.

    Please see the updated xVal post at

  86. Creating an error validation summary when using jquery validate is relatively simple, has demonstrations of this using errorLabelContainer.

    The difficulty in doing this with the xval / jquery integration is that the “display rules” are set in the _ensureFormIsMarkedForValidation function. Changing this from:

    _ensureFormIsMarkedForValidation: function(formElement) {
    if (!“isMarkedForValidation”)) {“isMarkedForValidation”, true);
    errorClass: “field-validation-error”,
    errorElement: “span”,
    highlight: function(element) { $(element).addClass(“input-validation-error”); },
    unhighlight: function(element) { $(element).removeClass(“input-validation-error”); }


    _ensureFormIsMarkedForValidation: function(formElement) {
    if (!“isMarkedForValidation”)) {“isMarkedForValidation”, true);
    errorLabelContainer: “#validationSummary”,
    wrapper: “li”,
    errorClass: “field-validation-error”,
    errorElement: “span”,
    highlight: function(element) { $(element).addClass(“input-validation-error”); },
    unhighlight: function(element) { $(element).removeClass(“input-validation-error”); }

    Changes the validation from inline to summary style ( and all you have to do is add a ul with the id validationSummary where you want the summary to appear). This does not address issue around server-side / client-side validation both appearing at the same time.

  87. Shane

    Great work Steve. Have you had any problems using Castle and Client Side validation? I’ve found that only my Int, Decimal, or Double gets picked up by ClientSideValidation. Strings, dates, enums and other properties do not.

    For instance, this class:

    public class MyClass
    public string MyString { get; set; }
    public int MyInt { get; set; }

    …would only produce the following:

    xVal.AttachValidator(“myClass”, {“Fields”:[{"FieldName":"MyId","FieldRules":[{"RuleName":"DataType","RuleParameters":{"Type":"Integer"}}]}]})

    My project works fine with Annotations, I just really wanted to use Castle. Any ideas?

  88. P.C

    Great work Steve,xVal sound very cool for validation.
    But now I’m using SharpArchitect with NHibernate.Validator, I tried to see your source code to reference again to this version of NHibernate.Validator, but I got stuck with exception in snippet below:

    case ValidatorMode.OverrideAttributeWithExternal:
    var xmlDefinition = GetXmlDefinitionFor(type);
    if (xmlDefinition != null)
    result = new XmlOverAttributeClassMapping(xmlDefinition);

    I’m new with NHibernate.Validator, can you help to give me instruction to fix this issue so. Tks much

  89. Will it be still useful after MVC 2 updates?

  90. hm, I am talking about DataAnnotation Validation Support for MVC V2. ;)

  91. Brendan

    I have downloaded the sample and I notice that validation is always peformed on the server; where does the client side validation come into it? I notice there is some javascript that looks to be related rendered at the bottom of the page, but clicking the button always results in validation occurring in the Controller?

  92. Brendan

    Just had to make some changes to the xVal.jquery.validate.js to get it to work correctly with web forms. Works very well.

  93. How do I add validation messages from resource files? Ideas on loclaization of these?

  94. Ray

    One thing I don’t get is why exceptions are used for validation. There are always going to be validation problems on a web site so shouldn’t we plan for those by not always throwing exceptions?

  95. CitizenBane

    Could you give us an example on how to validate a collection of items, client-side?

  96. hmm

    why doesn’t it actually work with custom messages(client side validation) if it supports castle and jquery

  97. Steve

    @Shane – sounds like you haven’t enabled the Castle validation provider. Also, make sure you upgrade to the newest version of xVal. See for details of both.

    @Victor – who can tell? Sounds like the MVC v2 model will be similar to xVal, so if you’re using xVal now, you can probably migrate to the MVC v2 native option fairly easily, or stick with xVal if you decide it suits your requirements better.

    @Dragan – use the DataAnnotations attributes – they let you specify your resource class and entry name if you want to localise your custom error messages. xVal will use the same properties to get your client-side messages in the current thread culture language.

    @Ray – in .NET, exceptions are the mechanism to signal that an operation wasn’t completed. For example, a record wasn’t saved because it violated some validation conditions. Why would you want to avoid that?

    @Nate – yes, if you use the “buddy class” mechanism (MetaDataTypeAttribute) to load your rules from that interface. See

    @CitizenBane – would love to, but blog posts take a long time to write, so there’s only so many I can manage. Hopefully I’ll cover this topic sometime soon.

    @hmm – make sure you’re using xVal 0.8 or newer.

  98. HeavyWave

    I don’t think this is a correct way of using exceptions. It is a validation not an “exception”, you are merely using it to indicate an error, an expected error. It has a significant performance overhead, especially in a deep code.
    So it would be nice to have exception-less alternative.

  99. Steve

    @HeavyWave – you’re entitled to your opinion, but I disagree. In the .NET framework class library, exceptions are used to indicate that an operation (such as saving a record) could not be completed, so this is exactly the sort of scenario ideal for their use. They are *not* reserved for especially catastrophic or unusual events.

    As for performance, try benchmarking the speed at which the CLR can throw and catch exceptions. Even on pitiful 5-year-old hardware, I’m sure you’ll manage 10,000 exceptions per second assuming there’s no debugger attached. This is far from your main performance bottleneck if you’re just throwing a few per request.

  100. Pingback: HowTo: Eingabenvalidierung in ASP.NET MVC | Code-Inside Blog

  101. Nimesh

    Thanks steve for this article..

    could you please help me in understanding whether xval supports mvc 1.0? i am using mvc with xval .8 version.
    is there any order for the jquery library?
    I have added jquery files in the order below:

    in few pages its breaking with some javascript error in IE 7.0, but works on Firefox.

  102. Dai Bok

    Thanks Steve for a Good Article, and the quickstart Tutorial.

    I have a question, similar to Ray above.

    Ray Says:
    August 13th, 2009 at 1:18 pm

    One thing I don’t get is why exceptions are used for validation. There are always going to be validation problems on a web site so shouldn’t we plan for those by not always throwing exceptions?


    I have also read this, Would it not be more efficient to use the ModelError – instantiated by the ModelError(string errorMessage ) – instead of an exception?

  103. Hello Steve,
    we have a problem.
    Your component is wild , but we are looking for some way to override some specific function in jquery.validate 1.5.5 that called showlabel , wich fires as a reaction to wrong input. we’d like it to show some image and tool tip instead of just text message in red color. The trouble is that just $.extend or even $.inherit plugin won’t do the job , because there are about 10 functions on the way , and anyway the xVal.jquery.validate uses jquery.validate specific file. Also , we would like to remain the jqueryvalidate file as it is , without changes , so that we could replace it when some more progressive version will pop out . What would you suggest , so that we could still use your plugin ?
    Thanks a lot , to begin with – you are doing a great job!

  104. Victor

    How could I add regexp as rule?
    I have the following code:
    xVal.AttachValidator(jQuery.format(“messageAttributes[{0}]“, messageAttributeCounter), {
    “Fields”: [
    { "FieldName": "Link",
    { "RuleName": "Required", "RuleParameters": {}, "Message": "Link is required." }
    }, {});
    And want to add second rule with regexp string “((mailto\:|(news|(ht|f)tp(s?))\://){1}\S+)”
    Should I do it as “Custom” rule and place this string instead of “RuleParameters”?

  105. Victor

    How to remove validation, that added via xVal.AttachValidator?

  106. Makoto

    Elegant solution, works well
    Thanks Steve for a great job !!

  107. Alberto

    Thanks Steve for your great job.

    Is there anybody here who can help me to translate this code:

    internal static class DataAnnotationsValidationRunner
    public static IEnumerable GetErrors(object instance)
    return from prop in TypeDescriptor.GetProperties(instance).Cast()
    from attribute in prop.Attributes.OfType()
    where !attribute.IsValid(prop.GetValue(instance))
    select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), instance);



  108. Alberto

    Thanks Steve for your great job.
    Is there anybody here who can help me to translate in VB.NET this code:
    internal static class DataAnnotationsValidationRunner
    public static IEnumerable GetErrors(object instance)
    return from prop in TypeDescriptor.GetProperties(instance).Cast()
    from attribute in prop.Attributes.OfType()
    where !attribute.IsValid(prop.GetValue(instance))
    select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), instance);

  109. Hearticy

    Thank you for your great work. I am using MVC + Entity Framework in our project. With XVAL to validate user’s input, this really saves us a lot of time.

    Now I have a probable. It seems that XVAL could not validate foreign key in a create or edit form because there’s no such foreign key property in entity model.
    Like the code below:
    public partial class AngleofReport
    public sealed class Metadata
    [Required(ErrorMessage = "Your message")]
    [RegularExpression(@"^[\u4e00-\u9fa5a-zA-Z0-9\s*-_()]+$”, ErrorMessage = “”)]
    public string Name
    [Required(ErrorMessage = "Error will occur here because FK_ReportItemAngle_ID is a foreign key . ")]
    public Guid FK_ReportItemAngle_ID

    Do you have any suggesetions? How can I validate the following foreign key?

    <%@ Page Title=”" Language=”C#” MasterPageFile=”~/Views/Shared/OpenWindow.Master” Inherits=”System.Web.Mvc.ViewPage” %>

    * Userage:


    Error message in the page:
    The associated metadata type for type ‘Model.ReportItem’ contains the following unknown properties or fields: FK_ReportItemAngle_ID. Please make sure that the names of these members match the names of the properties on the main type.

  110. C.T.

    I am trying to use xVal, I found I need to write such a method to get the DisplayName in the entity’s attribute

    public static string GetDisplayName(object instance, string propertyName)
    return TypeDescriptor.GetProperties(instance).Find(propertyName, true).DisplayName;

  111. Ryan

    Hi all!.
    Im working in an project with an old UI (.Net Framework 1.1) and I was thinking to redesign the UI business validation.

    I’m planning to move to .Net 3.5 (a hard project I think, pages are 1000 lines of code at least) but i was wondering if your framework can work with .Net 1.1. So i can plug it now and with the time migrate the whole application to .Net 3.5


  112. Steve

    Ryan, sorry, but xVal is only intended to work with ASP.NET MVC, which is itself built on .NET 3.5. It doesn’t work with WebForms 1.1 projects.

  113. ok9090

    very good ,很好。

  114. Hi,

    Could you provide an example for NHibernate.Validator???
    I´m trying here, but something is wrong, I´m using NHibernate.Validator 1.2, and I got the source code and updated following some posts on the web, and now I don´t know how to register and make it throw the erros…


  115. Vishal

    I have my validation decoration something like this.

    [Required(ErrorMessage = "required error"] [StringLength(15, ErrorMessage = "invalid error")]
    public string ClientName { get; set; }

    Now, the user would like to see the “required error” first, and the “invalid error” if the first validation does not fail.

    My question is, how does dataannotation decide which error to show (first or second). How can we control them? I did some testing to find that for the same scenarios I would get the required error or the invaild error erratically. How can I dictate the sequence in which the error message are considered for display.

  116. wboevink

    I thinks this is .net 4.0 related:

    var validationResults = new List();
    var vc = new ValidationContext(entity, null, null);
    var isValid = Validator.TryValidateObject(entity, vc, validationResults, validateAllProperties);

  117. neothemachine

    As the original DataAnnotationsValidationRunner didn’t check model errors but only prop errors and also didn’t pass the displayname to the error I updated it a little. Here it is, just in case someone needs it:

    internal static class DataAnnotationsValidationRunner
    public static IEnumerable GetErrors(object instance)
    var modelErrors =
    from a in TypeDescriptor.GetAttributes(instance).OfType()
    where !a.IsValid(instance)
    select new ErrorInfo(null, a.FormatErrorMessage(string.Empty), instance);

    var propertyErrors =
    from prop in TypeDescriptor.GetProperties(instance).Cast()
    from attribute in prop.Attributes.OfType()
    where !attribute.IsValid(prop.GetValue(instance))
    select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(prop.DisplayName), instance);

    return modelErrors.Union(propertyErrors);
    public static void ThrowErrors(object instance)
    var errors = GetErrors(instance);
    if (errors.Any())
    throw new RulesException(errors);

  118. Muhammad Adeel

    This was really a nice post. the thing i m trying to do is to bind my validations in models using data annotations and enforce them on client side using Html.EnableClientValidation() message. in MVC 2 it works fine for me. my concern here is to find out some way to control the event on which client side validations are displayed. by default it is onBlur() i suppose but i want to display validation errors (client side) onSubmit() event of the form.
    any help in this regard is highly apriciated

  119. John

    This way is good to validate presentation level data. But what if you need validate business level or dal level data. Once I read article and now use it in my projects. It is really flexible and because allow to validate any entity property.

  120. Yeah, One again,your articles is very good.thank you!very much. ;-)

  121. This is a very useful article you have, im going to add it to my website so i can link back to you easily.

  122. Ilir

    I thinks this is .net 4.0 related:

    var validationResults = new List();
    var vc = new ValidationContext(entity, null, null);
    var isValid = Validator.TryValidateObject(entity, vc, validationResults, validateAllProperties);

    @wboevink – This is the way .NET 4.0 validates the properties of the object based on the tags. It would be nice if there was a way to easily convert the ValidationResults into ErrorInfo and let the framework take care of the leg work of validating the target.

  123. David

    For the WebForms.Demo.WebSite example and running the BookingDemoMaster.aspx or any of them, I initially clicked the Submit button. I get the two client side error messages for Client Name and ArrivalDate in the validation summary as well as the red border textbox. When I select the client name textbox to enter data, the red border textbox for Arrival Date goes away. I don’t think it should be that way. Input has not been supplied for Arrival Date.

  124. Pingback: 关于 mvc validation «

  125. Ranjit Kumar

    Wish I could penetrate through the blackhole of DataAnnotationsValidationRunner.GetErrors. A double linq join is all it takes to scare the hell out of me, and don’t think I will be able to play with this toy, though it appeared like fun initially. I understand this is clever coding, but I am not there yet.

  126. escorts in quebec

    You are my aspiration , I possess few blogs and occasionally run out from to post : (.

  127. you could have an important blog right here! would you like to make some invite posts on my weblog?

  128. This is a very useful article you have, im going to add it to my website so i can link back to you easily.

  129. Hey was just bored and checkin around on other peoples blogs, nice post! Comment for comment? Thanks!

  130. Wow, amazing blog layout! How lengthy have you been running a blog for? you made running a blog look easy. The whole glance of your website is great, let alone the content!

  131. A person necessarily lend a hand to make critically articles I would state. That is the very first time I frequented your web page and thus far? I amazed with the analysis you made to make this particular submit incredible. Fantastic task!

  132. I clicked on a link and got here… probably not what I was looking for at the 1st place but I started reading and could not stop. Good Site!

  133. I know this isn’t the subject, but I have a wordpress blog using the same theme as yours and I get issues with the way my comments are displayed. is there a setting i’m lacking? Thanks.

  134. Hi!
    I download the W3 Total Cache plugin but
    this archive .js no exist:

    Can do you hel me please?

    I not looking it in the web

    Thank you