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

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

SimpleValidation

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

Recent comments

Comment RSS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008