The last few days I've been working on a MVC UI validation framework that closely follows that of ASP.NET. While my implementation is only in it's baby steps, I decided I would post it to the MVC Contrib project, and follow as much standards as what they have. Overall, the framework is very simple and straight forward in it's use.
Here were my requirements:
- It must closely match the ASP.NET UI validation framework.
- It should support all of the ASP.NET validators in their minimalistic implementation.
- It should require very minimal script inclusion or code to make this happen.
- It should only by a client side framework as you should be protecting your data on the server side through normal validations.
What I came up with, includes:
- It utilizes the ASP.NET UI validation framework by leveraging the WebUIValidation.js file (emitted through WebResource.axd).
- It supports all of the validators, with extended support for validation groups:
- RequiredValidator
- RegularExpressionValidator
- RangeValidator (currently does not support Date or Currency data types)
- CompareValidator
- CustomValidator
- It exposes 2 script inclusion calls, and 1 form validation setup call
- ValidatorRegistrationScripts() - Will render the WebUIValidation.js script, and scripts for form validation upon submission. This should be placed within the head tag.
- ValidatorInitializationScripts() - Will render all of the "expando" attributes for all validators upon the page and any initialization calls to set things up. This should be placed at the very end of your page before the closing body tag.
- FormValidation() - Will return an IDictionary object containing the "onsubmit" attribute. This should be called when creating your <form> tag.
- It is only a client-side framework leveraging ASP.NET's WebUIValidation.js
Since you now have an idea of the requirements and what was met, here's an example of it in action:
1: <%@ Import Namespace="MvcContrib.UI.Html" %>
2: <html>
3: <head>
4: <%= Html.Form().ValidatorRegistrationScripts() %>
5: </head>
6: <body>
7: <% using(Html.Form<MyController>(c => c.Save(), FormMethod.Post, Html.Form().FormValidation())) { %>
8: First Name: <%= Html.TextBox("firstName") %>
9: <%= Html.Form().RequiredValidator("firstNameRequired", "firstName", "First Name is Required.") %>
10: <%= Html.Form().RegularExpressionValidator("firstNameRegex", "firstName", "[a-zA-Z]*", "First Name can only contain letters.") %>
11: <br />
12: Age: <%= Html.TextBox("age") %>
13: <%= Html.Form().RequiredValidator("ageRequired", "age", "Age is Required.") %>
14: <%= Html.Form().RegularExpressionValidator("ageRegex", "age", "[0-9]*", "Age can only be numeric.") %>
15: <%= Html.Form().RangeValidator("ageRange", "age", "18", "35", ValidationDataType.Integer, "Age is not in target range of 18-35.") %>
16: <br />
17: <%= Html.SubmitButton("submit", "Save") %>
18: <% } %>
19: <%= Html.Form().ValidatorInitializationScripts() %>
20: </body>
21: </html>
At this point, I've posted it as a patch to MVC Contrib project, and hoping they apply it . If you would like to get your hands on it, check out the patches page, mine is #1063. Please let me know your input, I tried making it as simple as possible to use, and I believe I've achieved that.
Update: My patch has been applied by Eric :) Get the latest build (>= 0.0.1.96) it if you'd like to check this out. Also, let me know of any issues you may find by logging bugs on the CodePlex site.
1e92cdc0-2084-4398-9399-dc224a0beaa6|0|.0