jquery-validation-unobtrusive-bootstrap
jquery-validation-unobtrusive-bootstrap copied to clipboard
Issues with server-side added model state errors
Everything seems to work great with the validation annotations on the view model. However, I am having issues when, on the server-side (after a form post), I do some additional validations and add modelstate errors. Those errors actually render appropriately and come through in the response from the server, however, the errors that are associated with one of the properties/inputs on the form, those end up getting cleared out some how. Now, if I put the validation summary on the page, the message does show there, but the message related directly to the input doesn't show and the input isn't marked as invalid any longer. Any idea on what might be going on?
I'm using jQuery v2.1.4, jQuery Validation v1.14.0, jQuery Validation Unobtrusive v3.2.6, Bootstrap 4.4.1, and I am using your plugin v2.2.0.
the cshtml setup:
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" type="email" autocomplete="off" autofocus="true" />
_<span asp-validation-for="Email" class="invalid-feedback"></span>_
</div>
the server-side add model error statement:
this.ModelState.AddModelError(nameof(LoginViewModel.Email), "Your account has been disabled.");
the response as it came from the server to the client:
<div class="form-group">
<label class="control-label" for="Email">Email Address</label>
<input class="form-control input-validation-error" type="email" autocomplete="off" autofocus="true" data-val="true" data-val-required="The Email Address field is required." id="Email" name="Email" value="[email protected]" />
_<span class="invalid-feedback field-validation-error" data-valmsg-for="Email" data-valmsg-replace="true">Your account has been disabled.</span>_
</div>
This is from an inspection of the page after the page shows in the browser. As you can see the validation message that came from the server is gone and the input is cleared of the error:
<div class="form-group">
<label class="control-label" for="Email">Email Address</label>
<input class="form-control input-validation-error is-valid" type="email" autocomplete="off" autofocus="true" data-val="true" data-val-required="The Email Address field is required." id="Email" name="Email" value="[email protected]" style="" aria-required="true" aria-invalid="false" aria-describedby="Email-error">
<div id="Email-error" class="is-invalid invalid-feedback" style="display: block;"></div>
_<span class="invalid-feedback field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>_
</div>