0

how to validate MultiFieldRequired as one property to output one error message , and integrated with jquery client validation in mvc3 ?

i tried with MultiFieldRequired here , but there is no client validation message .

can fluentvalidation.net or rulesengine do this ?

thanks !

Community
  • 1
  • 1
dfang
  • 1,366
  • 15
  • 41

1 Answers1

0

If you don't care about the client side, on the server side you can implement IValidateableObject and provide single message validation that way.

public IEnumerable Validate(ValidationContext validationContext)
{
    if (string.IsNullOrEmpty(field1) || string.IsNullOrEmpty(field2))
    {
        yield return new ValidationResult("Your single validation message");
    }
}
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • He did say "and integrated with jquery client validation in mvc3" – Erik Funkenbusch Apr 20 '12 at 03:25
  • @Adam , then how can i get validationresult to put into modelstate – dfang Apr 20 '12 at 03:58
  • Thanks @MystereMan, I saw that. There's no other responses hence my post 'if you don't care' as they may actually not care as long as a validation result shows up to the client. Its an option, choose it or not, but you are more than welcome to post your own alternative. – Adam Tuliper Apr 21 '12 at 05:13
  • @dfang MVC will automatically call the interface methods and put the ValidationResult into ModelState assuming you are allowing the model binder to do its magic. I believe UpdateModel/TryUpdateModel will do this as well off the top of my head. – Adam Tuliper Apr 21 '12 at 05:15
  • @AdamTuliper i tried , it works , but how can i integrated with jquery client validation , can you give me some clue ? – dfang Apr 21 '12 at 11:35