Skip to content

Conversation

@dahlbyk
Copy link

@dahlbyk dahlbyk commented Sep 26, 2018

TL;DR: removes error elements in onReset.

It's not documented (question, question), but one can generally reset unobtrusive state with $(form).trigger('reset.unobtrusiveValidation'):

attachValidation: function () {
$form
.off("reset." + data_validation, onResetProxy)
.on("reset." + data_validation, onResetProxy)
.validate(this.options);
},

This handily includes a call to jQuery Validator resetForm():

$form.data("validator").resetForm();

However, it seems like the logic to reset each individual error should account for the fact that it might contain an error message:

$form.find(".field-validation-error")
.addClass("field-validation-valid")
.removeClass("field-validation-error")
.removeData("unobtrusiveContainer")
.find(">*") // If we were using valmsg-replace, get the underlying error
.removeData("unobtrusiveContainer");

Similarly, resetting .validation-summary-errors will leave <li>s behind:

$form.find(".validation-summary-errors")
.addClass("validation-summary-valid")
.removeClass("validation-summary-errors");

@mkArtakMSFT
Copy link
Contributor

@ryanbrandenburg, @javiercn can you guys please review this change? Thanks!

@ryanbrandenburg
Copy link
Contributor

The VSTS thing is something @natemcmaster had mentioned. He knows how to at least make it run.

@natemcmaster
Copy link

Updated branch protections since this repo is still using AppVeyor.

@ryanbrandenburg
Copy link
Contributor

@dahlbyk could you provide a repo which demonstrates the issue this PR is trying to solve? That would help me understand better what we're fixing and what other consequences it might have.

@dahlbyk
Copy link
Author

dahlbyk commented Oct 12, 2018

Use case is an add form (rendered in a modal, but not relevant) and submitted via AJAX. Model errors are parsed from a JSON response and passed through to $form.validate().showErrors(errors); this plugin renders those errors as expected.

The problem is resetting the form on success to add another. Resetting jQuery Validator via resetForm() works as expected, but triggering reset.unobtrusiveValidation leaves behind the error spans inside a .field-validation-valid container, e.g.

<span class="text-danger field-validation-valid" data-valmsg-for="Date" data-valmsg-replace="true">
 <span id="Date-error" class="">The value '123' is not valid for Date.</span>
</span>

I have worked around this with .field-validation-valid { display: none; }, but I'd prefer the DOM be truly reset.

(I'll try to put together a minimal repro, but it might be a few weeks.)

@danroth27 danroth27 added this to the 3.2.12 milestone Oct 17, 2018
@ryanbrandenburg
Copy link
Contributor

I'm going to close this as we don't have a repro. @dahlbyk if you're still interested in getting this change in please open a new PR and include a link to a repo which demonstrates the problem you're trying to solve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants