views:

413

answers:

1

There is a study here my co-worker took to my notice. Basically, that in-line form validation is a good thing.

But how would you do in-line multi-field form validation in MVC assuming you already have a "yield return" setup to return a list of form violations? Is the in-line validation only for primitive values like "a zip code should not include alpha characters?"

Would you submit some Javascript code to the client that checks that "this field and this field should be evaluated together firing this validation, and oh by the way, we are going to validate all fields again on a final submit?

Anyone have code example (C# and MVC) to illustrate handling in-line multi-field form validation using a remote repository (but not all fields at one time)?

+1  A: 

I don't have any code but if I was going to do inline validation I would implement the validation sample in Nerd Dinner.

Clearly this would validate on a submit so not really useful to your question. However, if you couple it with jQuery then it does become useful.

Essentially I'd be doing a jQuery postback at key points, checking for validation errors, and then highlighting the errors to the user in the callback.

You can attach events to say the lost focus events of fields that have a certains style class or to all fields etc. Really quite extensible in that regard.

There are tons or samples on jQuery and how to post back etc.

I'd also still be doing the full validation check on postback as well just to catch anything that may have been missed.

Does this help?

griegs
First of all, good answer +1. I think the client side MS Ajax extensions allow you to map server functions to JavaScript, so you could literally call them as if they were JavaScript Regex functions. I wonder if you can simply map these methods to the current MVC data validation. The key would be how to know which fields are validated together? Maybe send some sort of meta data along with the form that groups input fields by number? Wonder if there is some pattern for this?Nice answer!
Dr. Zim