xVal v1.0 Now Available
ASP.NET, MVC, xVal September 17th, 2009
xVal version 1.0 is released at last! In case you’re not aware, xVal is a validation helper for ASP.NET MVC that lets you use your own choice of server-side validation framework (e.g., Microsoft’s DataAnnotations attributes, or Castle Validator, or NHibernate Validaion) and dynamically generates client-side validation code from your rules.
You can download the library from http://xval.codeplex.com/. If you want a working example of xVal in action, download the demo project or read the rest of this blog post.
So, what’s new in version 1.0?
- Support for dynamic client-side validation summaries
- Remote validation (i.e., evaluating rules via Ajax)
- Additional language files for localised default validation messages: Danish, French, Dutch, Polish, Swedish, and Italian
- Upgraded to work with jQuery 1.3.2 and jQuery.Validation 1.5.5
- Fixes for some bugs and issues in version 0.8.
If you’d like to learn more about the basics of getting started with xVal, setting up rules, etc., check out my previous posts on the subject. In this post, I’ll describe only the new features and will assume you already know how to get started with xVal.
Client-side Validation Summaries
![]()
It’s the most commonly-requested feature, and it’s now here: you can display client-side validation messages in a “summary” format, just like ASP.NET MVC’s Html.ValidationSummary() helper does.
To do this, first make sure you’re rendering any server-generated validation messages into a standard ASP.NET MVC validation summary, and wrap that inside some element such as a DIV with some ID:
<div id="validationSummary"> <%= Html.ValidationSummary("Please fix the following problems:") %> </div>
Next, tell xVal that you want it to merge your client-generated messages into this summary by appending “.UseValidationSummary()” onto your existing call to Html.ClientSideValidation():
<%= Html.ClientSideValidation("booking", typeof(Booking)) .UseValidationSummary("validationSummary") %>
That’s it! Just make sure that, when you call UseValidationSummary(), you pass the ID of the element containing your server-generated validation summary.
Note: This feature requires you to be using xVal with jQuery Validation (not ASP.NET Native validation) because internally, it’s built on a jQuery Validation feature. xVal makes your client-generated messages and your server-generated messages seamlessly fit together in a single summary list.
Remote (Ajax) Validation
xVal already supported standard client-side rules, custom client-side rules, server-side rules, and rules defined as combinations of these. But what if you want a rule that behaves like a client-side rule (so it’s evaluated dynamically before the user submits the form) but its logic actually has to run on the server? For example, if you want a “username must be unique” rule, you can’t do that with pure JavaScript because it requires a database lookup to evaluate.
Not surprisingly, “remote” rules fill this gap. All you have to do is say that a certain field is subject to remote validation, and provide an action method that can be called to validate any given input. It’s usually best to add a RemoteRule to an existing collection of rules directly in your view:
<%= Html.ClientSideValidation("booking", typeof(Booking)) .AddRule("BookingDate", new RemoteRule(Url.Action("ValidateBookingDate"))) %>
Then, implement an action method that can validate some input. The action method will receive the complete contents of the form at the moment when validation is triggered, and can return its verdict in the form of a RemoteValidationResult, e.g.:
public RemoteValidationResult ValidateBookingDate(Booking booking) { var error = Booking.ValidateBookingDate(booking); return error == null ? RemoteValidationResult.Success : RemoteValidationResult.Failure(error.ErrorMessage); }
That does the job. For an example of this in action, download the v1.0 demo project.
Note: I suggest adding RemoteRules as ad-hoc rules in your views (using .AddRule() as shown above) rather than embedding them directly in your domain model like most other rules, because they need to know the URL of the action method that evaluates them, and that information doesn’t really belong in a good technology-agnostic domain model.
Note: This feature also requires you to be using xVal with jQuery Validation (not ASP.NET Native validation) because internally, it’s built on a jQuery Validation feature. If there’s strong demand for supporting remote validation with ASP.NET Native validation, it will be done for a future release.
How to upgrade
If you’re already using an earlier version of xVal, upgrading should only take a minute:
- Download the 1.0 release package from Codeplex and unzip it
- In your project, replace xVal.dll and xVal.jquery.validate.js and/or xVal.AspNetNative.js with the latest versions you just downloaded
What about ASP.NET MVC v2?
The good folks at Microsoft have already said that ASP.NET MVC v2 will come with built-in support for generating client-side validation code from rules in an arbitrary server-side validation framework. That’s a familiar idea…
So should you use that instead of xVal? If it turns out to meet your needs better then yes, of course, and if it doesn’t, then no. Use whatever suits you best!
However it will be quite a while before MVC v2 is released – currently it’s still on CTP 1 - so one option is to use xVal now and consider migrating at the same time that you upgrade your entire project to MVC v2 if indeed you choose to do that. The migration path from xVal to MVC v2’s built-in validation should be reasonably straightforward, since the two approaches appear to be rather similar, and you can always retain the option of sticking with xVal if it suits you better.
Mentions
Thanks to the following people who directly contributed to this release: House Air, Rodbv, Maarten Balliauw, Dariusz Tarczynski, Yoan80, Emmanuele, Eudega, and Howard van Rooijen.


September 17th, 2009 at 9:57 pm
Thats great news we’ve been using xVal for our latest project and love it.
The new remote AJAX validation stuff might come handy but the best feature is the client side ValidationSummary support.
Thanks for the hard work!
September 17th, 2009 at 11:26 pm
Thank you thank you thank you!!!! I’ve been playing with xVal for the last months and have loved it.
Am also loving your book btw.
September 18th, 2009 at 2:08 am
Awesome! Also know that I have found your MVC book to be the best out there and a great help in getting up to speed.
September 18th, 2009 at 7:36 am
[…] xVal v1.0 Now Available - Steve Sanderson announces the version1 release of xVal his Validation Helper for ASP.NET MVC. New in this release are support for dynamic client side validation summaries, support for remote ajax based validation, and localisation into a number of languages. This post also give a simple getting started tutorial […]
September 18th, 2009 at 12:23 pm
Congrats on the release!
September 18th, 2009 at 12:43 pm
This is excellent, I’ve found xVal to be a great tool, but what effect does the upcoming MVC2 built-in validation tooling mean for the future of xVal?
September 18th, 2009 at 12:51 pm
Thanks Steve!
I’ve been using xVal for a long time and it keeps getting better
I appreciate your time and effort to fill in a piece of important functionality that was never there 
September 18th, 2009 at 1:29 pm
Well done in releasing the first office version, I’ve been using it for the past four months, your efforts are appreciated
September 18th, 2009 at 3:44 pm
Is there documentation yet about the MVC2 way of validation? I haven’t seen it anywhere.
September 18th, 2009 at 7:11 pm
Are the client format rules internationalized? In line 140 of xval.jquery.validate.js I see a hardcoded regex for english formats. Is this the only way to handle DateTime in xval?
September 19th, 2009 at 1:55 am
Appears that xVal-1.0 and the NHibernateValidator RulesProvider was built against NHibernate.Validator Version=1.0.0.4000
The latest release version of NHibernate.Validator that works with the release of NHibernate v2.1 is NHibernate.Validator Version=1.2.0.2001…
Looks like others are wishing for a new xVal build… See: http://xval.codeplex.com/Thread/View.aspx?ThreadId=65033
Thanks for your great work on this project!
September 19th, 2009 at 11:11 am
Thanks everyone for your encouraging comments about xVal!
@mgroves, @Mike: MVC v2 is still a way off, so it’s too early to say.
@Mathias: For internationalization, on the server xVal will get error messages from localised resource files automatically, and on the client, it provides a simple way of changing the language used for default error messages (and provides sample translation files in about 10 languages). However, for datetime formatting, it does currently assume you’re using xx/xx/xxxx format. There is a plan to make this more flexible and easy to vary by thread culture, but that wasn’t enough of a priority for v1.0. If you’re keen, please consider submitting a patch.
@Scott - Yes, xVal 1.0 is built against the latest production version of NHibernate Validator. I did consider moving it to NHibernate Validator 1.2 Beta, but since that’s a beta version it seemed unfair on folks who are still on the latest NHibernate Validator production build. If you need, you can get the xVal source code and tweak it to reference NHibernate Validator 1.2 Beta. Otherwise I’m not sure how we can support both versions of NHibernate Validator simultaneously, considering that they have different APIs. Once NHibernate Validator 1.2 comes out of beta I’ll certainly update xVal to reference that by default.
September 20th, 2009 at 12:26 pm
…in an arbitrary server-side validation framework.
Does it mean MS will support any server-side validation framework (or its extensibility) or just its own in MVC v2?
Because I don’t want to be locked when I want to use NHibernate Validator or Fluent Validation for my POCOs, and turns out that I can’t using MS approach.
So thanks for xVal in this case! Already working with both!
September 21st, 2009 at 12:15 pm
That’s great news :). MVC 2 release is atleast a year away if not more. xVal works great meanwhile.
Thanks Steve. Awesome work.
September 22nd, 2009 at 3:05 pm
Very much appreciated, and very cool. Been using it for awhile and unless MS adopts your open approach I believe your framework will be the goto for people that want choices. Thanks again!
September 24th, 2009 at 8:40 am
Hello, can you provide small tutorial how to use it with WebForms for people, which isn’t on MVC route (yet)?
Thanx
September 26th, 2009 at 3:52 pm
你的博客好漂亮。精彩,顶一下!
September 28th, 2009 at 8:30 am
xVal v1.0 Now Available « Steve Sanderson’s blog…
Thank you for submitting this cool story - Trackback from Servefault.com…
October 1st, 2009 at 7:56 am
[…] Scott also showed how to use System.ComponentModel.DataAnnotations for validation and the client side validation which uses the jQuery validation framework. If you want that functionality now checkout Steve Sanderson’s excellent xVal framework. […]
October 5th, 2009 at 12:11 pm
Hi Steve,
In our project, we are using xVal framework for the validation. Some of our pages need to run on SSL. I tried using ASP.NET MVC Future’s RequireSsl attribute in the actions as below.
[AcceptVerbs(HttpVerbs.Post)]
[RequireSsl(Redirect = true)]
public ActionResult Register(RegisterModel registerModel)
The RegisterModel is a model object which contains all the xVal Validations.But i get the error in the xVal.AttachValidator while running it. This error occurs only when i add this attribute. Is there anything I am missing. Does the RequireSsl works with xVal validation?
Thanks.
October 20th, 2009 at 7:25 am
Hi Steve,
I am trying to upgrade xVal .8 version to xval 1.0 version.
i have downloaded the xval 1.0 package from the codeplex, i replaced the couple of files say,
xVal.AspNetNative.js
xVal.jquery.validate.js
While running the project, i am getting javascript error
“Microsoft JScript runtime error: ‘jQuery’ is undefined”
what are the files that i need to replace?
October 21st, 2009 at 10:12 am
Hello Steve,
well done, indeed. I’m playing with xVal for some time and it’s really great.
However, I have a small problem when a partial view with xVal validation is dynamically loaded to the DOM (jquery ajax to get the partial view from server). Problem is strange because validation actually works, just at the moment when partial view is loaded (elements returned from server and added to DOM) I get one alert for each validator on each field:
“Error: Element package_Name is not in a form”,
“Error: Element package_Description is not in a form” etc…,
If same partial view is loaded together with the page, everything works just fine.
Hope you can help with this issue…
Thanks!
October 27th, 2009 at 10:27 pm
thx, good thing
October 30th, 2009 at 2:56 am
[…] are a few basic steps to getting xVal available to your MVC project. This post by xVal’s creator, Steve Sanderson, is a great place to get this introductory […]
January 18th, 2010 at 11:58 pm
Hi, need a quick confirmation,
xVal doesn’t work with “System.Web.Mvc, Version=2.0.0.0″, right?
Thx, Vincent
January 19th, 2010 at 7:14 pm
Vincent,
xVal is mainly intended for use with ASP.NET MVC 1.0, because 2.0 has its own validation system that is similar and basically supersedes what xVal does. It should be possible to use xVal with MVC 2, but you may need to get the source code, change the reference to System.Web.Mvc.dll 2.0.0.0 and recompile.
January 25th, 2010 at 1:05 am
Thanks Steve, xVal looks very interesting. Would appreciate any tips or suggestions for adding the [Required] attributes when using the EF or LINQ to SQL OR/M designers. Seems that this is a problem, yes?
Possibly using the LINQ to SQL template for T4 at http://l2st4.codeplex.com/ might be a solution?
January 25th, 2010 at 9:41 am
Another Steve - What specific problem are you having with [Required]?
I know there’s been some debate lately about DataAnnotations’s [Required] and ASP.NET MVC 2’s model binder, but that isn’t anything to do with xVal. Or are you referring to something else?
January 25th, 2010 at 4:49 pm
It may be my lack of knowledge, but if I use the designer to create my entities how do I add the attributes to the properties? I’ve been led to believe that the designer created code should not be modified.
January 25th, 2010 at 4:54 pm
Another Steve - use the “buddy class” technique (i.e., [MetadataType]) as described at http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/