SimpleValidation.NET for ASP.NET MVC released

by Mathieu 25. August 2008 21:28

Still in alpha, and still a bit proof of concept, I released today a new version that supports ASP.NET MVC (Preview 4). Client side validation is handled by jquery.validation (http://docs.jquery.com/Plugins/Validation).

Currently, only required and email validators are implemented. Type validation is done server side. I will continue to add features during the week. Now, let's validate some users!

Still simple to use, all you have to do is :

Download the project on codeplex : http://www.codeplex.com/SimpleValidation

Create your entity (nothing new here!)

public class User
{
    private string name;
    private int age;
    private string email;
    private string password;
    private decimal size;

    [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}")]
    [ValidateType("Age format is not valid")]
    public int Age
    {
        get { return this.age; }
        set { this.age = value; }
    }

    [ValidateType("Size format is not valid")]
    public decimal Size
    {
        get { return this.size; }
        set { this.size = 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; }
    }

    [Confirm("Please confirm your password")]
    [Required("Password is required")]
    public string Password
    {
        get { return this.password; }
        set { this.password = value; }
    }
}

This one is self explanatory

Create your view

<%=Html.CreateValidators<Samples.Entities.User>("signupForm")%>

<%using( Html.Form("User", "New", FormMethod.Post, new Dictionary<string, object>() { { "id", "signupForm"} } ) ) { %>
    <div class="inputform">
        <fieldset>
            <legend>
                User
            </legend>
            <label>Name:</label>
            <div class="droite">
                <%= Html.TextBox("Name", ViewData["Name"] as string) %>
                <%= Html.ValidationErrors("Name") %>
            </div>
            
            <label>Password:</label>
            <div class="droite">
                <%= Html.TextBox("Password")%>
                <%= Html.ValidationErrors("Password")%>
            </div>
            
            <label>Confirm Password:</label>
            <div class="droite">
                <%= Html.TextBox("PasswordConfirm")%>
                <%= Html.ValidationErrors("PasswordConfirm")%>
            </div>

            <label>Age:</label>
            <div class="droite">
                <%= Html.TextBox("Age", ViewData["Age"] as string)%>
                <%= Html.ValidationErrors("Age")%>
            </div>
            
            <label>Size:</label>
            <div class="droite">
                <%= Html.TextBox("Size", ViewData["Size"] as string)%>
                <%= Html.ValidationErrors("Size")%>
            </div>

            <label>Email:</label>
            <div class="droite">
                <%= Html.TextBox("Email", ViewData["Email"] as string)%>
                <%= Html.ValidationErrors("Email")%>
            </div>

            <label>Confirm Email:</label>
            <div class="droite">
                <%= Html.TextBox("EmailConfirm", ViewData["EmailConfirm"] as string)%>
                <%= Html.ValidationErrors("EmailConfirm")%>
            </div>
            
            <div class="droite boutons">
                <input type="submit" value="Register" />
            </div>
        </fieldset>

    </div>
<%} %>

 

Don't forget to add a reference to jquery (http://jquery.com/) and jquery.validation (http://docs.jquery.com/Plugins/Validation)

Create your controller

public class UserController : Controller
{
    public ActionResult Index()
    {
        return View("New");
    }

    public ActionResult New()
    {
        User u = new User();

        if (!MvcValidationHelper.UpdateFrom(u, Request.Form, ViewData))
        {
            return View();
        }

        return View("UserCreated", u);
    }
}

 

And there you go !

kick it on DotNetKicks.com

Tags:

SimpleValidation

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