Search Wiki:
Toolkit works with ASP.NET Preview Release 3

Project Description

The Validator Toolkit provides a set of validators for the new ASP.NET MVC framework to validate HTML forms on the client and server-side using validation sets. By defining a validation set, e.g. a LoginValidationSet class, the toolkit will generate code, in conjunction with the JavaScript library jQuery, to validate on the client-side and will use the same set of rules to validate on the server-side.

Basically, you will create a validation set class which derives from ValidationSet base class:

public class LoginValidationSet : ValidationSet
{
	string Username = "";
	string Password = "";
 
	protected override ValidatorCollection GetValidators()
	{
		return new ValidatorCollection
		(
			new ValidateElement("username") { Required = true, MinLength = 5, MaxLength = 10 },
			new ValidateElement("password") { Required = true, MinLength = 3, MaxLength = 10 }
		);
	}
 
	protected override void OnValidate()
	{
		if(Username == "Bill" && Password == "Jobs")
			throw new ValidatorException("username", "The username/password combination is not valid");
	}
}

Then, you will attach it to the view and the HTML form processing action using the ValidationSetAttribute:

public void Login()
{
	RenderView("Login");
}
 
[ValidationSet(typeof(LoginValidationSet))]
public void Authenticate()
{
	if(this.ValidateForm())
		RenderView("Ok");
	else
		RenderView("Login");
}
 
...
 
[ValidationSet(typeof(LoginValidationSet))]
public partial class LoginView : ViewPage
{
}

Then, you add the following script and methods to your view:

<script type="text/javascript">
	$(function(){
		updateSettingsForLoginValidationSet($('#loginForm').validate({rules:{
));
});
</script>

...

<form id="loginForm" action="/Authenticate" method="post">
...
</form>

<% this.RenderValidationSetScripts(); %>
}}
This all to validate the login HTML form on the client and server-side.

More documentation take a look at the article on CodeProject.com: http://www.codeproject.com/KB/aspnet/MvcValidatorToolkit.aspx

Please see the source code with the included sample site for more examples. You will also find a multi-form example showing how to use the toolkit in conjunction with multiple forms on one HTML page (view).
Last edited Jun 20 at 8:56 AM  by jbaurle, version 14
Comments
ovannovitch wrote  Jan 29 at 4:39 PM  
Thanks! Looks interesting!

dariog wrote  Mar 28 at 9:05 PM  
Looks interesting but it's too difficult.

SteveGentile wrote  Apr 2 at 2:50 AM  
very nice. I would like it to support the validation methods available from the validation library ( http://docs.jquery.com/Plugins/Validation/Methods )

ie. new ValidateElement("EmailAddress") { email = true }

Good work

hnchan wrote  May 7 at 8:29 AM  
Is it possible that to override the error message?

TBermudez wrote  Jun 19 at 8:10 PM  
Any way to update this to work with MVC Preview 3?

Updating...