Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with server-side added model state errors #9

Open
richleah opened this issue Sep 21, 2020 · 0 comments
Open

Issues with server-side added model state errors #9

richleah opened this issue Sep 21, 2020 · 0 comments

Comments

@richleah
Copy link

richleah commented Sep 21, 2020

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant