Project Goal

Provide simple validation for entities

Usage :

Create your entity, with each property tagged with the desired validation :

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; }
    }
} 

With ASP.NET MVC

... create your view : ...

Do not forget to add a reference to jquery (http://www.jquery.com) and jquery.validate (http://docs.jquery.com/Plugins/Validation)

<%=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>

           [...]
            
            <div class="droite boutons">
                <input type="submit" value="Register" />
            </div>
        </fieldset>

    </div>
<%} %>

... 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);
    }
} 

With WebForms :

... create your form ...

<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>

... add the WebValidationHelper control ...

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

... and the corresponding validators are added automatically !

example1.png
Last edited Aug 25 2008 at 7:18 PM by mathieub, version 4

 

Want to leave feedback?
Please use Discussions or Reviews instead.

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Version 2009.6.1.15196