Click here to Skip to main content
Licence CPOL
First Posted 16 Jan 2012
Views 10,322
Bookmarked 6 times

Announcing MVC3 Validation Groups

By | 16 Jan 2012 | Technical Blog
The new MVC3 Validation Groups.
A Technical Blog article. View original blog here.[^]

I’ve recently had to organise the validation for long/multipart forms. Ordinarily, you’d divide the form up into smaller models and create a wizard style user journey. But in this case the UI design called for the user to arbitrarily jump back and forth between pages in the wizard and other restrictions meant that we couldn’t persist each sub-page on change. What we needed were validation groups!

As this work involved a bit of the old copy-paste-adapt from the standard MVC code, it’s a bit complicated to go into the fine details of how to replicate the functionality for yourself. So I’ve setup a CodePlex project with a reusable library all ready to go! (Of course all the source code is up there as well!)

How to use

  1. Download the latest release from codeplex. (Get the latest Example Site as well to see a preconfigured MVC3 Website and example integration)
  2. Add a reference to ‘McjDevelopment.Mvc3ValidationGroups.dll’ to your MVC3 project
    image
  3. Copy ‘jquery.validate.unobtrusive.validationgroups.js’ to your ‘scripts’ folder
    image
  4. Decorate your view models with the [ValidatorGroup] attribute specifying the validation groups that that field belongs to (space delimited)
    image
  5. Update your view to use ‘jquery.validate.unobtrusive.validationgroups.js’ instead of ‘jquery.validate.unobtrusive.js’ (or ‘jquery.validate.unobtrusive.min.js’)
    image
  6. Update your view to include where you want each groups Validation Summary to appear
    image
  7. Amend your ‘Action’ buttons and links to include ‘data-val-valgroup-name’ attribute and specify which validation groups should be triggered by the user clicking on that element
    image
  8. Amend your controller to check the appropriate validation groups when called  (this implementation shows a common action being called with uses the clicked button name to select the validation group, an alternative is to us JS to dynamically alter the form’s action location).
    image
  9. Test validation groups!
    image

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Martin Jarvis

Software Developer (Senior)
Freestyle Interactive Ltd
United Kingdom United Kingdom

Member

Follow on Twitter Follow on Twitter
I'm a lead developer for Freestyle Interactive Ltd where we create many wonderful websites built on Microsofts ASP.Net and Ektron CMS.
 
I've been developing .Net applications (both Windows and Web) since 2002.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
NewsFixed having to modify the standard js files in version 0.2.0.0 PinmemberMartin Jarvis9:53 17 Jan '12  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 16 Jan 2012
Article Copyright 2012 by Martin Jarvis
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid