03.06
I’ve been neglecting xVal for WebForms for a while now, mainly because I’m not sure which direction to take it. The xVal project is now deprecated in favor of the client side validation support introduced in ASP.NET MVC 2. This is obviously a problem since xVal for WebForms is built on top of xVal.
I think there are a few directions the project could take. The more traditional WebForms approach would be to simply generate the standard System.Web.Web.UI validation controls based on a model’s DataAnnotation attributes. I’m a fan of this approach as it makes a lot of sense to WebForm developers. For example, consider the following model:
public class Booking { [Required(ErrorMessage = "Client Name is required.")] public string ClientName { get; set; } [Range(1, 20, ErrorMessage = "Number of Guests must be between 1 and 20.")] public int NumberOfGuests { get; set; } }
The generated validators would be very similar to the following:
<asp:RangeValidator ID="valNumberOfGuests" runat="server" Display="Dynamic" ControlToValidate="txtNumberOfGuests" Type="Integer" MinimumValue="1" MaximumValue="20" ErrorMessage="Number of Guests must be between 1 and 20."/> <asp:RequiredFieldValidator ID="valClientName" runat="server" Display="Dynamic" ControlToValidate="txtClientName" ErrorMessage="Client Name is required." />
The other option is to find a way use the ASP.NET MVC validation bits with WebForms. ASP.NET MVC 2 uses the MicrosoftAjax library, while ASP.NET MVC 3 introduced unobtrusive validation with jQuery Validate. This approach would work a lot like the current project, but it would utilize MVC instead of xVal. I have to admit I’m not thrilled about referencing System.Web.Mvc in a WebForms project.
Yet another option is to generate jQuery validation scripts from scratch, using the methods Dave Ward has suggested. I like this idea since it doesn’t put a dependency on MVC bits.
It took me a while, but after working with WebForms and trying to make validation better, i.e. more like MVC, I can’t help but think that the best option is to simply move to MVC. But xVal for WebForms can still help projects that can’t be converted to MVC. I’ve already created a branch prototyping the first, most WebForms friendly option. If you use xVal for WebForms or are interested in attribute based client and server side validation, please let me know which option you would prefer.
I would prefer solutions that use the jQuery validation plugin over the ASP.NetSystem.Web.Web.UI validation controls.
The entity framework 4.1 now contains server side validation built in. Maybe a jQuery validation client side to the server side validation would be useful for people?
Does EF 4.1 use DataAnnotations for server side validation? I’ll have to check that out.
jQuery Validation does seem to be more flexible that web forms validation.
Yes, I prefer data annotations based validation for asp.net webforms. and your xval for webforms doing well. But I more prefer solutions that use Jquery validation plugin instead of normal asp.net validator. However, I trying to do something to simulate jquery validation like ASP.NET MVC.
Hi first of all thank you for your great work with xval , i download it from nuget version 0.4 but it seems that it have a bug when i use Data annotations regular expressions all the other validation works so please if you can help me i get the yellow screen with Object reference not set to an instance of an object
Stack trace
[NullReferenceException: Object reference not set to an instance of an object.]
xVal.WebForms.RulesJavaScriptConverter.b__1(Rule rule) +0
System.Linq.Enumerable.ToDictionary(IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) +225
xVal.WebForms.RulesJavaScriptConverter.Serialize(RuleCollection rules, JavaScriptSerializer serializer) +159
xVal.WebForms.RulesJavaScriptConverter.Serialize(Object obj, JavaScriptSerializer serializer) +26
Thank you in advance
Would you mind including your aspx markup and your model class? You can post a comment here or create an issue at http://xvalwebforms.codeplex.com. Thanks!
[...] on comments I’ve received from my last xVal for WebForms post, I’ve decided on a direction. The project will keep jQuery Validation but will move away from [...]