views:

75

answers:

3

Hi all,

I've had a good look around and am certain that there's no matching question on SO, so here goes.

Has anyone created a 'helper' method on their model that generates jquery (or plain javascript) rules validation dynamically, based on the criteria/rules that are contained within the object and taken from a repository (i.e. DB).

What i'm thinking of is a discrete set of partial views (and associated models) that have rules at the business logic 'level' and rather than (or in combination with) validating the rule(s) at postback, translating the same rules into tightly focussed jquery methods that work identically at client (js) and server (c#) levels. I can see benefits here re performance. Also, the rules definitions could be created in a single place (in c#) and the jquery generated off of that, thus allowing single edits to update both code streams.

I appreciate that there would be limitations imposed by language specific contstraints but the general principle could be quite interesting if used appropriately. I'm also aware that testibility could be an issue when using two different language structures and hoping to achieve similar test outcomes - but those aside...

any thoughts or experiences of similar out there??

cheers jimi

edit - this article goes some way towards what i was tthinking of:

http://blog.stevensanderson.com/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/

and also:

http://devermind.com/aspnet-mvc/asp-net-mvc-tip-3-how-to-cover-all-your-client-side-form-validation-needs-without-writing-any-javascript-part1/

+1  A: 

It depends on what you mean by "rules". If you mean basic validation, that already exists with the DataAnnotations attributes.

These attributes, when applied properly, can combine with Html.ValidateMessageFor helper methods to automatically validate input before the form is posted back.

Dave Swersky
Hi Dave,I was thinking more of the enforcing of business logic at client level as opposed to validating that a decimal had been entered etc..
jim
I don't think you want business logic pushed out to the client like that. Not all browsers support javascript, and those that do don't all implement it the same way.
Dave Swersky
Dave, my understanding is that the functionality degrades gracefully in the absence of js. also, as per the above link to snadersons article, there's a kinda hybrid approach in play, so really the best of both worlds should (in theory) be being tapped into.
jim
In cases where javascript is unavailable, you rely on the server to handle the validation, yes. I still wouldn't call the validation you link to in your question "business rules." They're data validation rules, which are much simpler. You can't afford to allow your actual business logic degrade, gracefully or not.
Dave Swersky
Dave - yes, totally agreed re not being able to allow business rules to degrade. i'm kinda hoping to find a path that would allow more complex rules to be 'tested' client side but degrade and/or switch to serverside where necessary. not an easy path i grant you.
jim
+1  A: 

Codebetter.com has a good tutorial with a MVC validation solution that builds the jQuery validation

http://codebetter.com/blogs/karlseguin/archive/2009/04/26/validation-part-1-getting-started.aspx

Glennular
glen - not a bad article. still feel that there's a fully formed idea out there waiting to discover 'me' tho :)
jim
+1  A: 

Have a look at this article, ASP.NET MVC 2 Custom Validation, by Phil Haack. In it he describes how to add client side validation for custom validation attributes.

HTHs,
Charles

Charlino
Charlino - taking a look now, thanks..
jim
good article from PH. more interestingly, found a link in the comments that shows almost exactly the scenario that i was imagining:http://devermind.com/aspnet-mvc/asp-net-mvc-tip-3-how-to-cover-all-your-client-side-form-validation-needs-without-writing-any-javascript-part1/
jim