4

I've got a problem that I can't figure how to solve.
I have a remote validation in a model, just like this:

[Required]
[Display(Name = "Social Security Number:")]
[Remote("IsSocialSecurityNumberValid", "Applicant", ErrorMessage = "Invalid.")]
public string SocialSecurityNumber { get; set; }

But there's another validation that I would like to apply, that is:

[Remote("SocialSecurityNumberExists", "Applicant", ErrorMessage = "Already exists.")]

But mvc doesn't let me add two remote attributes. How could I solve that?
Thanks for the help.

5
  • 3
    Create one "general" remote validation Action, that return the combined result of the IsSocialSecurityNumberValid and SocialSecurityNumberExists methods. Commented Dec 2, 2013 at 13:10
  • But for each case i want a different message... how would i do that? Commented Dec 2, 2013 at 13:22
  • I can't think of a simple solution that does not require tinkering with the metadata or some sort of JavaScript manipulation. If a generic "SSN invalid or existing" message is out of the question, you would need to resort to metadata manipulation, which, in controller, is not a thrill... Commented Dec 2, 2013 at 13:30
  • Well, javascript doesn't seems to me a bad approach. So do you think it's a good idea to call the server twice in a javascript function and show the correct validation through two different actions? I had thought about that, but was afraid of doing some "kludge"... Commented Dec 2, 2013 at 13:48
  • 1
    Why twice? Just call it once (on the "general" remote validation Action) and expect an "error message" as result. Then set it on your property. Commented Dec 2, 2013 at 13:54

1 Answer 1

13

See below an example:

    [Required]
    [Display(Name = "Social Security Number:")]
    [Remote("ValidSocialSecurityNumber", "Applicant")]
    public string SocialSecurityNumber { get; set; }

Your Action

public JsonResult ValidSocialSecurityNumber([Bind(Prefix = "SocialSecurityNumber ")] string ssn) 
{
    if (!isSocialSecurityNumberValid) 
    {
        return Json("Invalid.", JsonRequestBehavior.AllowGet);
    }
    if (isSocialSecurityNumberExists) 
    {
        return Json("Already exists.", JsonRequestBehavior.AllowGet);
    }
    return Json(true, JsonRequestBehavior.AllowGet);
}
Sign up to request clarification or add additional context in comments.

6 Comments

But I think in this case I'm still validating only one of the cases. The additional attribute should be a different one...
hi @GiordanoGiuliano, I got it know. Let me update my answer.
Using only one remote validation function, you can return different error messages.
Oh nice one. Pretty simple. I'm gonna try it and tell you if it works. Thank you very much for the help. I'm still a starter at MVC. ^_^
No problems. We are all learning. You need to implement isSocialSecurityNumberValid and isSocialSecurityNumberExists before use them. hope it helps.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.