5

I am using ASP.NET MVC3 with jQuery Validate + the unobtrusive validation support that comes with MVC3. Works great for almost everything, but I have one view where there is some view-wide validation that I need to do and I am not sure how to tap into the validation events that happen as part of MVC3+Validate+Unobtrusive. The actual validation will take just a few lines of code. I just don't know where to plug in that code.

I would like to tie into existing validation flow so that when the standard validation finds errors with individual fields and adds warning messages for them, my form-wide error message will also appear in the validation summary.

P.S. I am clear how to add appropriate model-level validation on the server side (Scott just blogged about it), but I feel it would be strange if some validation happened on the client and others only on the server. A user might see the client validation errors, fix them, then try to submit the form and only then get the model-level validation error message.

marcind
  • 52,944
  • 13
  • 125
  • 111
Erv Walter
  • 13,737
  • 8
  • 44
  • 57

2 Answers2

1

I would say what you are looking for is the new support for Remote Validation in Asp MVC3. Here is an article describing a common scenario, hopefully you can extend it yourself. Otherwise there are probably other articles around explaining it even better ;-)

http://www.aaronstannard.com/post/2010/12/07/remote-validation-asp-net-mvc3.aspx

/Victor

Victor
  • 3,999
  • 3
  • 24
  • 27
  • Remote validation could work, but I need the values of multiple fields (which interact) in order to do the validation logic. – Erv Walter Dec 11 '10 at 08:06
  • Sure, but that shouldnt really be a problem, should it? Look at the PasswordsMustMatchAttribute here for example: http://forums.asp.net/p/1625928/4189265.aspx - When applied to the class (viewmodel) you can check all properties in that class. – Victor Dec 12 '10 at 16:36
-2

You should ALWAYS validate again on the server. Its really easy to circumvent javascript validation.

Rule #1 of Web Development: NEVER TRUST USER INPUT

John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • 7
    While your answer is good advice, it doesn't really have anything to do with the question. He's wondering how to add custom validation across the model as a whole (not asking how to do javascript-only validation). – jessegavin Dec 10 '10 at 16:19
  • I know. I'm responding to this line: "but I feel it would be strange if some validation happened on the client and others only on the serve" Its scary to read. – John Farrell Dec 10 '10 at 16:30
  • 2
    Of course it will be re-validated on the server. That doesn't make client validation useless. What my statement meant was that it is an odd user experience if some of your fields are validated on both the client and server and others are validated on only the server. Users see a list of errors they need to fix from the client validation and think they are done, then they get additional errors from the server when they actually submit. – Erv Walter Dec 11 '10 at 08:02