First release of SimpleValidation.NET, an ASP.NET validation framework

by Mathieu 3. July 2008 22:08

SimpleValidation.NET is a simple validation (!) framework, designed to work with ASP.NET and entities (or domain objects, or whatever you like :)). Its primary goal was simplicity of usage, and as i'm a big fan of "Show, dont tell" :

First, you have to create your entity. It can be an NHibernate mapped entity, for example. After that, you just tag it's properties :

public class UserEntity
{
    private string name;
    private int age;
    private string email;
 
    [Required("Name is required")]
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }
            
    [Required("Age is required")]
    [Range(0, 99, "Age must be in range {0} {1}")]
    public int Age
    {
        get { return this.age; }
        set { this.age = value; }
    }
 
    [Email("Email is invalid")]
    [Confirm("Please confirm your email")]
    [Required("Email is required")]
    public string Email
    {
        get { return this.email; }
        set { this.email = value; }
    }
}

After that, add a simple form to your page :

<div runat="server" id="inputForm" class="inputform">
<label>Name:</label>
<asp:TextBox runat="server" ID="Name" />
<label>Age:</label>
<asp:TextBox runat="server" ID="Age" />
<label>Email:</label>
<asp:TextBox runat="server" ID="Email" />
<label>Confirm Email:</label>
<asp:TextBox runat="server" ID="EmailConfirm" />
<asp:Button runat="server" ID="btn" Text="Test" />
</div>

And the WebValidationHelper control :

<SimpleValidation:WebValidationHelper ID="WebValidationHelper1" runat="server" 
EntityType="Samples.Entities.UserEntity, Samples.Entities" 
ValidatorDisplay="Dynamic"
ParentControl="inputForm" />

And here we go : corresponding validators are added automatically !

example1

How is it working ?

Very simply : you have to name your inputs with the same name as your entity's properties (convention over configuration here). Then, validators are added after the input control by default. You can also do the validation manually, or specify placeholders (they are located by prefix, like "ValidatorsForName"). For more details, you can look at the samples included in source code.

The project is hosted on codeplex here : http://www.codeplex.com/SimpleValidation/

kick it on DotNetKicks.com

Tags: ,

SimpleValidation

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen