Disable Submit Button Until Required Fields are Filled Out
Disable submit buttones until all required fields have been filled out. Currently only supports single-page forms.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gw-disable-submit-button-until-required-fields-are-filled-out.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Gravity Wiz // Gravity Forms // Disable Submit Button Until Required Fields are Filled Out
*
* Disable submit buttones until all required fields have been filled out. Currently only supports single-page forms.
*
* @version 1.2
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link https://gravitywiz.com/...
* @copyright 2013 Gravity Wiz
*/
class GW_Disable_Submit {
public static $script_output = false;
public $form_id;
public function __construct( $form_id ) {
$this->form_id = $form_id;
add_action( 'gform_pre_render', array( $this, 'maybe_output_script' ) );
add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ) );
}
public function maybe_output_script( $form ) {
if ( $form['id'] != $this->form_id ) {
return $form;
}
if ( ! self::$script_output ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
self::$script_output = true;
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
var GWDisableSubmit;
(function($){
GWDisableSubmit = function( args ) {
var self = this;
// copy all args to current object: formId, fieldId
for( prop in args ) {
if( args.hasOwnProperty( prop ) )
self[prop] = args[prop];
}
self.init = function() {
$( args.inputHtmlIds.join( ', ' ) ).change( function() {
self.runCheck();
} );
// Check for newly uploaded files
args.inputHtmlIds.forEach(function (inputHtmlId){
let searchTerm = "#gform_preview";
let index = inputHtmlId.indexOf(searchTerm);
if ( index !== -1 ) {
let fieldId = inputHtmlId.substring(index + searchTerm.length).trim();
window.gfMultiFileUploader.uploaders['gform_multifile_upload' + fieldId].bind('StateChanged', function() {
if ( window.gfMultiFileUploader.uploaders['gform_multifile_upload' + fieldId].state == 1 ) {
self.runCheck();
}
});
}
});
// Check if field becomes empty on deleting file(s)
$(document).on( 'click', '.gform_delete_file', function() {
self.runCheck();
});
// Check when fields are repopulated because of GPPA
$(document).on( 'gppa_updated_batch_fields', function() {
self.runCheck();
});
// GPPA logic may enable submit button, disable that logic.
gform.addFilter( 'gppa_disable_form_navigation_toggling', function() {
return true;
});
self.runCheck();
}
self.runCheck = function() {
var $form = $( '#gform_' + self.formId );
var submitButton = $form.find( 'input[type="submit"], input[type="button"], button[type="submit"]' );
if( self.areRequiredPopulated() ) {
submitButton.attr( 'disabled', false ).removeClass( 'gwds-disabled' );
} else {
submitButton.attr( 'disabled', true ).addClass( 'gwds-disabled' );
}
}
self.areRequiredPopulated = function() {
var inputs = $( args.inputHtmlIds.join( ', ' ) ),
inputCount = inputs.length,
fullCount = 0;
$( inputs ).each( function() {
var input = $( this ),
fieldId = input.attr( 'id' ).split( '_' )[2];
// don't count fields hidden via conditional logic towards the inputCount
if( window['gf_check_field_rule'] && gf_check_field_rule( self.formId, fieldId, null, null ) == 'hide' ) {
inputCount -= 1;
return;
}
if( $.trim( $( this ).val() ) ||
$( this ).find( 'input:checked' ).length ||
$( this ).find( '.ginput_preview' ).length
) {
fullCount += 1;
}
} );
return fullCount == inputCount;
}
self.init();
}
})(jQuery);
</script>
<?php
}
public function add_init_script( $form ) {
if ( $form['id'] != $this->form_id ) {
return $form;
}
$args = array(
'formId' => $form['id'],
'inputHtmlIds' => $this->get_required_input_html_ids( $form ),
);
$script = '; new GWDisableSubmit( ' . json_encode( $args ) . ' );';
$slug = "gw_disable_submit_{$form['id']}";
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function get_required_input_html_ids( $form ) {
$html_ids = array();
foreach ( $form['fields'] as &$field ) {
if ( ! $field['isRequired'] ) {
continue;
}
$input_ids = array();
switch ( GFFormsModel::get_input_type( $field ) ) {
case 'address':
$input_ids = array( 1, 3, 4, 5, 6 );
break;
case 'name':
$input_ids = array( 1, 2, 3, 4, 5, 6 );
break;
case 'fileupload':
// Only multiple files enabled File Upload field has to be handled differently.
if ( rgar( $field, 'multipleFiles' ) ) {
$html_ids[] = "#gform_preview_{$form['id']}_{$field['id']}";
break;
}
// Let single file enabled File Upload field be handled as default.
default:
$html_ids[] = "#input_{$form['id']}_{$field['id']}";
break;
}
if ( ! $input_ids ) {
continue;
}
foreach ( $input_ids as $input_id ) {
$html_ids[] = "#input_{$form['id']}_{$field['id']}_{$input_id}";
}
}
return $html_ids;
}
}
# Configuration
new GW_Disable_Submit( 534 );
Hello, I can’t get this snippet to work. I’ve added it via the ‘snippets’ plugin like all the others but it doesn’t disable the button. I thought that maybe this has to be changed to the form #:
new GW_Disable_Submit( 534 );
… but that didn’t make a difference. Any thoughts before I submit a support ticket?
Thanks
Hi
I just tested the snippet and it works as expected. The 534 in the configuration is supposed to be the form ID. If it’s still not working for you, please submit a ticket for this via our support form, so we can take a closer look at your setup and troubleshoot this.
Best,
Hi Dave, I think the title has a typo: Disable Submit Button Until Required Fields are Field Out where FIELD should be Filled….maybe.
Hey Brian,
You’re absolutely right! Nice catch, and thanks for bringing this to our attention. We’ll get that sorted.🙂