I am trying to implement remote validation using ASP.NET MVC3 unobtrusive javascript.
It seems to work, but I am able to submit my form even if it is not supposed to be valid.
It looks like the validation is not happening fast enough. I am using the ASP.NET development server with the VS 2010 debugger and the remote validation method is not always fired. When I wait a little, the validation happens and I cannot submit the form.
I am starting to think that client-side remote validation is not reliable enough and I maybe should consider using server-side validation just to be sure the correct validations are applied.
Is there a way to fix this?
EDIT:
Like you asked, here are the part of my code relative to the problem. I will start by taking your advice and use server-side validation to avoid users bypassing my validations.
Model:
[Required(ErrorMessage = "*"), StringLength(50)]
[Remote("EventCategoryNameExists", "EventCategories",
AdditionalFields = "EventCategoryId",
ErrorMessageResourceType = typeof(Messages),
ErrorMessageResourceName = "EventCategoryNameAlreadyExists")]
[LocalizedDisplayName("Name")]
public string Name { get; set; }
View:
<div id="formMain">
@Html.HiddenFor(x => x.EventCategoryId)
<fieldset class="formFieldset">
<legend>@Labels.EventCategoryDetails</legend>
<div class="formFieldsetContent">
<table id="formTable" class="formTable">
<tr>
<td class="formLabelCell" style="width: 90px;">
@Html.LabelFor(x => x.Name) :
</td>
<td class="formInputCell">
@Html.EditorFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
</td>
</tr>
<tr>
<td class="formLabelCell" style="vertical-align: top;">
@Html.LabelFor(x => x.Color) :
</td>
<td class="formInputCell">
@Html.EditorFor(x => x.Color)
@Html.ValidationMessageFor(x => x.Color)
</td>
</tr>
</table>
</div>
</fieldset>
</div>
<div class="formButtons">
<input type="submit" id="btnSave" value="@Labels.Save" class="formButton" />
<input type="button" id="btnCancel" value="@Labels.Cancel" class="formButton" />
</div>
Controller:
public ActionResult EventCategoryNameExists(int eventCategoryId, string name)
{
return Json(!_eventService.EventCategoryNameExists(eventCategoryId, name), JsonRequestBehavior.AllowGet);
}