Is there any way to set different errors according with logic in MVC 3.0 remote validation
public ActionResult IsUserEmailExists(string email)
{
bool isExists = service.IsUserExists(email);
if(isExists )
//Set error message
return Json(!isExists, JsonRequestBehavior.AllowGet);
else if(something)
//another logic
//Set errror message
return Json(something, JsonRequestBehavior.AllowGet);
}
By default Remote validation using only ErrorMessage value from attribute declaration
[Remote("IsUserEmailExists", "Account", ErrorMessage = "User with such email already exists")]
is there any way to change that behavior?
You could return the error message instead of a boolean value:
return Json("Some custom error message", JsonRequestBehavior.AllowGet);
In this case the model will be considered invalid (the same as if you had returned false
) and the string used as error message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With