Skip to content

fix for issue #430 #436

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jquery.validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,13 +669,14 @@ $.extend($.validator, {
},

showLabel: function(element, message) {
var label = this.errorsFor( element );
var label = this.errorsFor( element ),
okToReplace = label.attr("generated") || label.hasClass(this.settings.errorClass);
if ( label.length ) {
// refresh error/success class
label.removeClass( this.settings.validClass ).addClass( this.settings.errorClass );

// check if we have a generated label, replace the message then
if ( label.attr("generated") ) {
if ( okToReplace ) {
label.html(message);
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ <h3></h3>
<input class="productInfo" name="color">
<input class="productInfo" type="checkbox" name="discount" />
</form>

<form id="updateLabel">
<input class="required" name="updateLabelInput" id="updateLabelInput" data-msg-required="You must enter a value here" />
<label id="targetLabel" class="error" for="updateLabelInput">Some server-side error</label>
</form>

</div>

Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1280,3 +1280,25 @@ test("Specify error messages through data attributes", function() {
var label = $('#dataMessages label');
equal( label.text(), "You must enter a value here", "Correct error label" );
});


test("Updates pre-existing label if has error class", function() {
var form = $('#updateLabel'),
input = $('#updateLabelInput'),
label = $('#targetLabel'),
v = form.validate(),
labelsBefore = form.find('label').length,
labelsAfter;

input.val('');
input.valid();
labelsAfter = form.find('label').length;

// label was updated
equal( label.text(), input.attr('data-msg-required') );
// new label wasn't created
equal( labelsBefore, labelsAfter );
});