4

What is the best way to do validation in MVC 3? Here are the requirements:

  1. Works client and server side.
  2. Shares as much code between client and server as possible (attribute on model property seems ideal)
  3. Works across async request
  4. Display errors, validation messages, and success messages coming from the server side
  5. Unobtrusive javascript, as minimal as possible
  6. Dynamically added HTML should still validate the same way

My task this weekend is to build a robust solution for this, figured I'd ask here first before re-inventing or re-discovering the wheel with blood sweat and tears.

Milimetric
  • 13,411
  • 4
  • 44
  • 56
  • I'd be curious to see what recommendations people have for this. I've practically given up on using data annotations and unobtrusive client validation--I feel like it's only a matter of time before I come across a situation that's too complex for it to handle elegantly. I almost exclusively use [FluentValidation](http://fluentvalidation.codeplex.com/) now because it's so versatile, but I usually only do server side validation. – ataddeini Jun 04 '11 at 13:33
  • I'm curious too. [This post on how to add more client side validators when using custom DataAnnotations might help you some](http://www.a2zdotnet.com/View.aspx?Id=183), though I don't think it answers everything. – Tridus Jun 04 '11 at 14:44

1 Answers1

1

I would check out Brad Wilson's blog on this. He covers using unobtrusive validation in MVC3, sounds like exactly what you're looking for.

Adding more info per OP's comment

Regarding server side validation (custom validation), check out @jfar's response to a similar question I posted regarding custom validation -- he suggests that you should question your design if you're relying heavily on custom validation. In my case, I ended up going either with Ajax to handle my custom validation, or allowed the postback to perform the validation.

Community
  • 1
  • 1
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
  • Hm, I had read that before. It's good but doesn't address all my requirements. In re-reading it I actually thought of another (added #6 on my list of requirements). The post doesn't cover how to display server side error messages, validation errors, or success messages. Doesn't cover dynamically added HTML, and it's not as minimal as possible - technically jQuery.validate should be the only thing required if I understand its API correctly. – Milimetric Jun 04 '11 at 14:16
  • @Milimetric - I updated the post re: server-side validation. Can you clarify, though, what you mean by "validation errors" and "success messages"? DataAnnotations gives you control of the error messages used in your validation, if that's what you referring to. Not sure what you mean by success messages, though. If you're referring to a confirmation (i.e. "Your record has been successfully updated"), then I'm not sure that's a concern of validation. – Jerad Rose Jun 04 '11 at 14:36
  • thank you for the update. Error messages from the server would be like "timeout" or "saved 3 records but failed 1" and highlight the failed record. I know this might be a lot to ask, but I feel like this has to do with validation because validation is the problem we need to solve in order to have elegant, dry solutions to any kind of form we can throw at the system. This is definitely possible with a lot of legwork and manual code, I'm just wondering if there's any best practice. It sounds like maybe there isn't. I might just write a sample project and post here. – Milimetric Jun 04 '11 at 15:52