Skip to content

Commit cce5ac8

Browse files
phistuckstaabm
authored andcommitted
Core: Added support for button elements with descendants (jquery-validation#1901)
Non-Firefox browsers dispatch "click" on descendants of <button>, which breaks the submitHandler logic that adds a hidden field with the name and value of the submit button (since it is not included in the submission if it did not initiate the submission, which might be the case when using form.submit in submitHandler and returning false). submitButton is instead a descendant of <button> and the hidden field does not compensate for the lack of an actual submit button. This would fix crbug.com/668524 and make submitButton an actual button. * Added HTML for button with descendants test For testing that clicking on descendants of a button element still keeps the button element as the submit button. * Added a test for button with descendants support For testing that clicking on descendants of a button element still keeps the button element as the submit button. * Removed an extraneous submission from the test Accidental copy-paste. * Fixed some style warning Forgot to wrap the parenthesized value with spaces. * Fixed a style warning Forgot to wrap a parenthesized value with spaces. * Fixed a style warning Forgot to add a blank line above a comment. * Fixed the button with descendants test Replaced a complex event initiation with a basic click(). * Fixed a style warning Removed extraneous trailing spaces. * Fixed some style warnings Removed two unused variables. * Fixed a submitButton regression Instead of returning a DOM element, a jQuery element was returned. Changed to return the original element in the target is already a button case. * Simplified the button with descendants support Instead of looking for the parent or ancestor of the target, just used the currentTarget property. * Added another test Devised by @Arkni.
1 parent 7457cc6 commit cce5ac8

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ $.extend( $.fn, {
2727

2828
this.on( "click.validate", ":submit", function( event ) {
2929
if ( validator.settings.submitHandler ) {
30-
validator.submitButton = event.target;
30+
validator.submitButton = event.currentTarget;
3131
}
3232

3333
// Allow suppressing validation by adding a cancel class to the submit button

test/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ <h3></h3>
435435
<option value="2016">2016</option>
436436
</select>
437437
</form>
438+
<form id="testForm27">
439+
<input name="year"/>
440+
<button name="submitForm27" value="someValue" type="submit"><span>Submit</span></button>
441+
</form>
438442
</div>
439443
</body>
440444
</html>

test/test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,50 @@ QUnit.test( "submitHandler keeps submitting button", function( assert ) {
397397
$( "#userForm" ).submit();
398398
} );
399399

400+
QUnit.test( "submitHandler keeps submitting button, even if descendants are clicked", function( assert ) {
401+
var button;
402+
403+
$( "#testForm27" ).validate( {
404+
debug: true,
405+
submitHandler: function( form ) {
406+
407+
// Dunno how to test this better; this tests the implementation that uses a hidden input
408+
var hidden = $( form ).find( "input:hidden" )[ 0 ];
409+
assert.deepEqual( hidden.value, button.value );
410+
assert.deepEqual( hidden.name, button.name );
411+
412+
return false;
413+
}
414+
} );
415+
$( "#testForm27 [name=\"year\"]" ).val( "2016" );
416+
button = $( "#testForm27 :submit" )[ 0 ];
417+
$( button ).find( "span" ).click();
418+
} );
419+
420+
QUnit.test( "submitHandler keeps submitting button", function( assert ) {
421+
var button = $( "#testForm27 :submit" )[ 0 ];
422+
var v = $( "#testForm27" ).validate( {
423+
debug: true,
424+
submitHandler: function( form ) {
425+
426+
// Compare the button with the `submitButton` property
427+
assert.deepEqual(
428+
v.submitButton, button, "The submitButton property should be the same as button"
429+
);
430+
431+
var hidden = $( form ).find( "input:hidden" )[ 0 ];
432+
assert.deepEqual( hidden.value, button.value );
433+
assert.deepEqual( hidden.name, button.name );
434+
435+
return false;
436+
}
437+
} );
438+
439+
$( "#testForm27 [name=\"year\"]" ).val( "2016" );
440+
441+
$( button ).find( "span" ).click();
442+
} );
443+
400444
QUnit.test( "validation triggered on radio/checkbox when using keyboard", function( assert ) {
401445
assert.expect( 1 );
402446
var input, i, events, triggeredEvents = 0,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy