I am trying to implement optional client side validation using
- ASP.NET MVC 4,
- unobtrusive jQuery validation,
- unobtrusive ajax
This works fine
Following pictures show what I mean with optional client side validation: The only one field on my form is expected to contain email, so there is an email validator attached to it.
Now we click on save. Because our text "name@doamin" is not a valid email the validation summary gets displayed. Together with validation summary we are unhiding "Save anyway" button.
This second button is a normal submit button just having class="cancel"
. This instructs jQuery.validate.js
script to skip validation when submitting using this button.
Here is the code snippet of the view:
@using (Html.BeginForm())
{
<div>
<input data-val="true" data-val-email="Uups!" name="emailfield" />
<span class="field-validation-valid" data-valmsg-for="emailfield" data-valmsg-replace="false">*</span>
</div>
<input type="submit" value="Save" />
@Html.ValidationSummary(false, "Still errors:")
<div class="validation-summary-valid" data-valmsg-summary="true">
<input type="submit" value="Save anyway" class="cancel" />
</div>
}
These all works fine.
The problem
The second submit button - "Save anyway" stops working as expected if I switch over to Ajax form. It just behaves like the the normal one and prevents submit until validation succeeds.
Here is the code snippet of the "ajaxified" view:
@using (Ajax.BeginForm("Edit", new { id = "0" },
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "ajaxSection",
}))
{
<div>
<input data-val="true" data-val-email="Uups!" name="emailfield" />
<span class="field-validation-valid" data-valmsg-for="emailfield" data-valmsg-replace="false">*</span>
</div>
<input type="submit" value="Save" />
@Html.ValidationSummary(false, "Still errors:")
<div class="validation-summary-valid" data-valmsg-summary="true">
<input type="submit" value="Save anyway" class="cancel" name="saveAnyway" />
</div>
}
I have debugged jQuery.validation.js
to find out what is the difference, but failed.
Qustion
Any ideas to fix or workaround the problem and let the ajax form behave as intended are welcome.
Addendum
To have a client side validation is an absolute must. Server side validation is not an option. Aside from higher traffic (this sample is a simplification - the real form contains much more fields) and latency, there is one thing which server side validation would not do: client side validators highlight erroneous fields on lost focus. It means you have a feedback as soon as you tab to the next field.