Site Meter
 
 

xVal v1.0 Now Available

image 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.

image

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:

  1. Download the 1.0 release package from Codeplex and unzip it
  2. 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.

kick it on DotNetKicks.com

42 Responses to xVal v1.0 Now Available

  1. Naz

    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!

  2. Gabriel Florit

    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.

  3. Lance Harper

    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.

  4. Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #436

  5. Ira

    Congrats on the release!

  6. 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?

  7. 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 ;)

  8. Well done in releasing the first office version, I’ve been using it for the past four months, your efforts are appreciated

  9. Mike

    Is there documentation yet about the MVC2 way of validation? I haven’t seen it anywhere.

  10. Mathias Fritsch

    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?

  11. Scott Wade

    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!

  12. Steve

    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.

  13. cowgaR

    …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!

  14. That’s great news :) . MVC 2 release is atleast a year away if not more. xVal works great meanwhile.

    Thanks Steve. Awesome work.

  15. 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!

  16. Petr

    Hello, can you provide small tutorial how to use it with WebForms for people, which isn’t on MVC route (yet)?

    Thanx

  17. 你的博客好漂亮。精彩,顶一下!

  18. Pingback: Servefault.com

  19. Pingback: ASP.NET MVC V2 and I met Scott Gu | Arrange Act Assert

  20. Gayathri

    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.

  21. Nimesh

    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?

  22. Misha N.

    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!

  23. Pingback: ASP.NET MVC Auto Model Validation with xVal « Corey Coogan

  24. Vincent

    Hi, need a quick confirmation,
    xVal doesn’t work with “System.Web.Mvc, Version=2.0.0.0″, right?
    Thx, Vincent

  25. Steve

    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.

  26. Another Steve

    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?

  27. Steve

    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?

  28. Another Steve

    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.

  29. Steve

    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/

  30. Juliana

    Hey, this is so much better than MVC 2 built-in validation. I did have a problem using it with MVC2. I had to download the source code and rebuild it with MVC2 assemblies. Can’t you create a release that is already compiled with MVC2? Thanks!

  31. Forget about the client-side validation in ASP.NET MVC 2.0 (final/RTM/however you like to call it). I’m sorry Haack and the team, but it’s horrible.

    Use xVal instead.

    Why?

    1. xVal doesn’t force you to have the domain models in the same assembly with the views nor reference System.Web.Mvc in the separate assembly. This means cleaner code / architecture.

    2. It’s way simpler.

    3. Remote validation doesn’t have to suck. Just compare the remote validation way of ASP.NET MVC 2 : http://bradwilson.typepad.com/blog/2010/01/remote-validation-with-aspnet-mvc-2.html

    Doesn’t look horrible?

    —— BOTTOM LINE ——-

    USE XVAL.

  32. Hi Steve,

    Even though we now have MVC 2.0, I prefer xVal because of its ease of use and the flexibility we have with ad hoc rules, and of course the Remote Validation feature.

    The only thing I’m having a troubles with is the DOM location where the validation message is created: exactly next to the input field (e.g. …), where the element is dynamically created.

    But what I really want is to be able to control where the validation message appears, for example in another table column:

    Is that possible?

  33. This is very nice.

    I’m coming from the ASP.NET WebForms world, and am used to having a CompareValidator control. I normally use this type of control for a “Confirm Password” type UI, i.e., typically on a Registration form, I would ask the user to enter a password and then re-enter it for confirmation.

    Is it possible to compare two fields using xVal?

    Cheers.

    Ross

  34. Thanks for the information!, undecided Certainly through every affiliate marketer’s comments, nonetheless it remanns to be seen the way in which this will work

    Web Design Milton Keynes

  35. Greetings, My name is Kris Larsen and i am an avid Metal Detectorist, i made a decision to make several web sites around the hobby of metal detecting and especially around those that are well worth buying and that you ought to prevent.

  36. Hi, My name is Kris Larsen and i am a passionate Metal Detectorist, i am choosing to build several sites about the hobby of metal detecting and especially around which ones are well worth buying and that you must stay away from.

  37. swim is best for us, car rearview mirror, car dvd

  38. Eskort bayanlar eskort evlicift bay bayan partner kamerali sesli sohbet odalarinda sende eskort arkadasinla tanıs guzellikleri paylas

  39. aslında gerçekten hayata sokulabilinecek bir proje desteklenirse

  40. xVal v1.0 Now Available – Steve Sanderson’s blog – As seen on YouTubeâ„¢ I was suggested this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are wonderful! Thanks! your article about xVal v1.0 Now Available – Steve Sanderson’s blog – As seen on YouTubeâ„¢Best Regards Shane