I am using jQuery templates for a bunch of the site I'm working on, and have reached the point where I need to decide how to go about implementing validation, clientide. Serverside is an Asp.Net Mvc Restful service that handles various models. Models are styled by DataAnnotations to describe validation. The following is an example (note that we are using the resource file to copy the error):
[Required( ErrorMessageResourceType = typeof(Validation), ErrorMessageResourceName = "HomeAddressRequired")] [StringLength(250, ErrorMessageResourceType = typeof(Validation), ErrorMessageResourceName = "HomeAddressStringLength")] public string Address { get; set; }
I used to just use Razor (or the old Asp.Net viewer until I switched to Mvc 3), which handled the creation of all the client decorations needed to connect jquery.validate.js. Now when I use jQuery templates, this is not so viable.
Here I see three options:
Modify the templates to manually enable validation and a copy of the error . This sucks because I will be forced to separately check client and server checks; it kind of kills the whole idea of DataAnnotations.
Use an unobtrusive javascript approach to using Razor and Mvc 3 to help me render templates . Better since it reproduces validation for me, but with a performance hit. Also means that I use the server-side template engine ... to render templates. I can minimize performance with output caching.
Use an exclusively remote confirmation that returns model states for my models and writes my own adapter for jQuery validation to handle routing errors in the corresponding element . It has the ability to use Razor, but it kills a clean client check and means I have to write a complex plugin.
Ideally, when we deploy this thing, I should be able to serve the templates for the client as a static file without any direct dependency on the backend.
So, to my question: Using jQuery clientside and DataAnnotations templates on my serveride models, what is the most efficient means of processing validation without repeating itself?
Dusda source share