I am using JustValidate library (https://github.com/horprogs/Just-validate) for form validation. It works perfectly on the client-side, but seems to not work properly when executing remote validation. For instance: If we have an input field "email" the remote validation in our Javascript would look like the code below.
rules: {
email: {
email: true,
remote: {
url: '@Url.Action("Validate", "ActivityTypes")',
sendParam: 'email',
successAnswer: 'OK',
method: 'GET'
}
}
}
Every time user inputs something in the e-mail input, value is sent to server and checked if it's okay or not. The problem is you always get a response OK. And every time you get that response the error message for the input shows (Even if the input has been filled out correctly). My controller action looks like this:
public async Task<IActionResult> Validate(ActivityType values)
{
var activityType = await _context.ActivityType
.FirstOrDefaultAsync(m => m.Name == values.Name);
if (activityType == null)
{
return NotFound();
}
return Ok();
}
I have already tried redirecting from one action to another in the controller, returning NotFound() but am still getting the Ok response, and the error message shows up again. When trying the demos on the JustValidate library's GitHub page I noticed that inputs are always showing error messages for the input "email", even when they shouldn't. Has anyone found a workaround? I have already used this library on 80% of all my form and don't want to change it for some other library.
Since you mentioned successAnswer: 'OK' you need to return the response as Content. Like this.
[HttpGet]
[Route("check-exists")]
public IActionResult CheckExists(string email)
{
return email == "anuraj@example.com" ? Content("Not Okay") : Content("OK");
}