4

I need to do some model-level validation in an MVC 3 edit page. (To be specific, I need to confirm that either Field A or Field B is filled in, but not both and not neither.)

I want to perform client-side validation as well as server-side validation, which means either using remote validation or implementing duplicate validation code. I'm OK with either.

I've read a number of posts on rolling your own server-side model-level validation, but none that deal with also implementing client side validation. (I don't know -- I'm sure someone out there can tell me -- whether model-level client-side validation is easy to set up with jQuery validation.)

I've also read about implementing your own remote validation from scratch, which I may have to do since the Remote attribute is property-level only.

I've read this question, which is identical to mine, but the only link that's really on-point doesn't seem to say what the answerer says it says.

So, my question: is there an easy, relatively low-effort way to implement server+client model-level validation, with or without a remote component? And is there a nice blog post or web page somewhere that explains this?

Community
  • 1
  • 1
Ann L.
  • 12,802
  • 5
  • 30
  • 60

3 Answers3

1

I think Data Annotation Extention by Scott Kirkland does exactly what you want. Here is a blog post he wrote about his extensions.

The core library provides server-side validation attributes that can be used in any .NET 4.0 project (no MVC dependency). There is also an easily pluggable client-side validation library which can be used in ASP.NET MVC 3 projects using unobtrusive jquery validation (only MVC3 included javascript files are required).

Ron Sijm
  • 7,502
  • 2
  • 26
  • 43
  • Thank you very much! But ... as far as I can tell, these are all property-level validations. I was hoping for something I could use at the model level, for complex multi-field situations. Thank you very much, though! – Ann L. Nov 10 '11 at 21:11
0

If i get you right, mvc 3 do actually include jquery client-side validation. First, for the model-level server side validation, you can override the default isValid function with your own validation rules, something like this (involve multi-field):

 public sealed class PropertyAAttribute : ValidationAttribute
{
    public string propertyBAttribute { get; set; }
    public override bool IsValid(object value)
    {
        // Your validation rule here
    }
}

[PropertyA(propertyBAttribute = "PropertyB")]
public object PropertyA {get;set;}
public object PropertyB {get;set;}

Then, to deal with client side, you can simply use the included jquery validation function:

            var frm = $('#formData');

            frm.validate();

Like this, you will have the error message at client side based on the rule you defined in the model. Hope this is what you need :)

shennyL
  • 2,654
  • 9
  • 38
  • 64
0

There is a similar question answered here which may be of some help? The answer given is for validating that at least one field is entered but the principles given in the answer may be what you are looking for and you should be able to change the answer to suit the validation you require. The solution also offers both server and client side validation options and I believe you can use the solution as both module or property level validation?

Additionally, there is also the following article here detailing how to create your own custom validation similar to that provided in the answer I linked to.

Hope this helps.

Community
  • 1
  • 1
Dangerous
  • 4,590
  • 3
  • 29
  • 47