Skip to content

Commit e0876fb

Browse files
Arknistaabm
authored andcommitted
Core: Move the logic of adding hidden input from stopeRequest to handle
The only scripted submits in this plugin are triggered by `stopRequest()` when the remote method finished validating an element and the form was submitted
1 parent 4a9a927 commit e0876fb

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/core.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ $.extend( $.fn, {
2626
if ( validator.settings.onsubmit ) {
2727

2828
this.on( "click.validate", ":submit", function( event ) {
29+
2930
// Track the used submit button to properly handle scripted
3031
// submits later.
31-
validator.submitButton = event.target;
32+
validator.submitButton = event.currentTarget;
3233

3334
// Allow suppressing validation by adding a cancel class to the submit button
3435
if ( $( this ).hasClass( "cancel" ) ) {
@@ -50,17 +51,22 @@ $.extend( $.fn, {
5051
}
5152
function handle() {
5253
var hidden, result;
53-
if ( validator.settings.submitHandler ) {
54-
if ( validator.submitButton ) {
5554

56-
// Insert a hidden input as a replacement for the missing submit button
57-
hidden = $( "<input type='hidden'/>" )
58-
.attr( "name", validator.submitButton.name )
59-
.val( $( validator.submitButton ).val() )
60-
.appendTo( validator.currentForm );
61-
}
55+
// Insert a hidden input as a replacement for the missing submit button
56+
// The hidden input is inserted in two cases:
57+
// - A user defined a `submitHandler`
58+
// - There was a pending request due to `remote` method and `stopRequest()`
59+
// was called to submit the form in case it's valid
60+
if ( validator.submitButton && ( validator.settings.submitHandler || validator.formSubmitted ) ) {
61+
hidden = $( "<input type='hidden'/>" )
62+
.attr( "name", validator.submitButton.name )
63+
.val( $( validator.submitButton ).val() )
64+
.appendTo( validator.currentForm );
65+
}
66+
67+
if ( validator.settings.submitHandler ) {
6268
result = validator.settings.submitHandler.call( validator, validator.currentForm, event );
63-
if ( validator.submitButton ) {
69+
if ( hidden ) {
6470

6571
// And clean up afterwards; thanks to no-block-scope, hidden can be referenced
6672
hidden.remove();
@@ -1082,20 +1088,16 @@ $.extend( $.validator, {
10821088
delete this.pending[ element.name ];
10831089
$( element ).removeClass( this.settings.pendingClass );
10841090
if ( valid && this.pendingRequest === 0 && this.formSubmitted && this.form() ) {
1085-
var hidden;
1086-
// If no submit handler is set but a submit button is present
1087-
// ensure the following scripted submit has the value of the
1088-
// submit button by injecting a hidden field.
1089-
if ( !this.settings.submitHandler && this.submitButton ) {
1090-
hidden = $("<input type='hidden'/>")
1091-
.attr("name", this.submitButton.name)
1092-
.val($(this.submitButton).val())
1093-
.appendTo(this.currentForm);
1094-
}
10951091
$( this.currentForm ).submit();
1096-
if ( hidden ) {
1097-
hidden.remove();
1092+
1093+
// Remove the hidden input that was used as a replacement for the
1094+
// missing submit button. The hidden input is added by `handle()`
1095+
// to ensure that the value of the used submit button is passed on
1096+
// for scripted submits triggered by this method
1097+
if ( this.submitButton ) {
1098+
$( "input:hidden[name='" + this.submitButton.name + "']", this.currentForm ).remove();
10981099
}
1100+
10991101
this.formSubmitted = false;
11001102
} else if ( !valid && this.pendingRequest === 0 && this.formSubmitted ) {
11011103
$( this.currentForm ).triggerHandler( "invalid-form", [ this ] );

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