Validate that a Value Exists
Ensure that a value entered in Form A has been previously submitted on Form B. This is useful if you’re generating a reference number of some sort on Form B and would like the user to enter it on Form A.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gw-validate-that-a-value-exists.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/**
* Gravity Wiz // Gravity Forms // Validate that a Value Exists
*
* Ensure that a value entered in Form A has been previously submitted on Form B. This is useful if you're generating a reference number of some sort
* on Form B and would like the user to enter it on Form A.
*
* @version 1.10
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link https://gravitywiz.com/require-existing-value-submission-gravity-forms/
*/
class GW_Value_Exists_Validation {
protected static $is_script_output = false;
private $_args = array();
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'target_form_id' => false,
'target_field_id' => false,
'source_form_id' => false,
'source_field_id' => false,
'validation_message' => __( 'Please enter a valid value.' ),
'field_map' => array(),
'disable_ajax_validation' => false,
'validate_blank_values' => true,
) );
// Map source and target fields to field map if field map is not set.
if ( empty( $this->_args['field_map'] ) ) {
$this->_args['field_map'] = array( $this->_args['source_field_id'] => $this->_args['target_field_id'] );
}
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// make sure we're running the required minimum version of Gravity Forms
if ( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
return;
}
add_filter( 'gform_validation', array( $this, 'validation' ) );
if ( ! $this->_args['disable_ajax_validation'] ) {
add_action( 'gform_enqueue_scripts', array( $this, 'enqueue_form_script' ) );
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ) );
add_action( 'wp_ajax_gwvev_does_value_exist', array( $this, 'ajax_does_value_exist' ) );
add_action( 'wp_ajax_nopriv_gwvev_does_value_exist', array( $this, 'ajax_does_value_exist' ) );
}
}
public function enqueue_form_script( $form ) {
if ( $this->is_applicable_form( $form ) ) {
wp_enqueue_script( 'gform_gravityforms' );
}
return $form;
}
public function validation( $result ) {
if ( ! $this->is_applicable_form( $result['form'] ) ) {
return $result;
}
foreach ( $result['form']['fields'] as &$field ) {
if ( $this->is_applicable_field( $field ) && ! GFFormsModel::is_field_hidden( $result['form'], $field, array() ) ) {
if ( ! $this->do_values_exists( $this->get_values(), $this->_args['source_form_id'] ) ) {
$field['failed_validation'] = true;
$field['validation_message'] = $this->_args['validation_message'];
$result['is_valid'] = false;
}
}
}
return $result;
}
public function ajax_does_value_exist() {
if ( ! wp_verify_nonce( rgpost( 'nonce' ), 'gwvev_does_value_exist' ) ) {
die( __( 'Invalid nonce.' ) );
}
$form_id = rgpost( 'form_id' );
$values = rgpost( 'values' );
$entries = $this->get_matching_entry( $values, $form_id );
echo json_encode( array(
'doesValueExist' => ! empty( $entries ),
'entries' => $entries,
) );
die();
}
public function does_value_exist( $value, $form_id, $field_id ) {
$entries = $this->get_matching_entry( array( $field_id => $value ), $form_id );
return count( $entries ) > 0;
}
public function do_values_exists( $values, $form_id ) {
$entries = $this->get_matching_entry( $values, $form_id );
return $entries && count( $entries ) > 0;
}
public function get_matching_entry( $values, $form_id ) {
$field_filters = array();
foreach ( $values as $field_id => $value ) {
$field_filters[] = array(
'key' => $field_id,
'value' => $value,
);
}
$args = apply_filters( 'gwvev_get_entries_args', array(
$form_id,
array(
'status' => 'active',
'field_filters' => $field_filters,
),
) );
$entries = GFAPI::get_entries( $args[0], $args[1] );
return reset( $entries );
}
public function get_field_map() {
if ( ! empty( $this->_args['field_map'] ) ) {
$field_map = $this->_args['field_map'];
} else {
$field_map = array( $this->_args['target_field_id'] => $this->_args['source_field_id'] );
}
return $field_map;
}
public function get_values() {
$field_map = $this->get_field_map();
$values = array();
foreach ( $field_map as $source_field_id => $target_field_id ) {
$value = rgpost( 'input_' . $target_field_id );
if ( ! rgblank( $value ) || $this->_args['validate_blank_values'] ) {
$values[ $source_field_id ] = $value;
}
}
return $values;
}
public function load_form_script( $form, $is_ajax_enabled ) {
// Do not output main script if AJAX is enabled
if ( ! $is_ajax_enabled && $this->is_applicable_form( $form ) && ! self::$is_script_output && ! $this->is_ajax_submission( $form['id'], $is_ajax_enabled ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
window.GWValueExistsValidation = function( args ) {
var self = this;
// copy all args to current object: (list expected props)
for( prop in args ) {
if( args.hasOwnProperty( prop ) ) {
self[ prop ] = args[ prop ];
}
}
self.init = function() {
self.$elems().on( 'change', function() {
var inputId = gf_get_input_id_by_html_id( $( this ).attr( 'id' ) );
for( var sourceFieldId in self.fieldMap ) {
if( self.fieldMap.hasOwnProperty( sourceFieldId ) && sourceFieldId == inputId ) {
break;
}
}
self.doesValueExist( $( this ) )
} );
};
self.getAllValues = function() {
var values = {};
self.$elems().each( function() {
var inputId = gf_get_input_id_by_html_id( $( this ).attr( 'id' ) );
for( var sourceFieldId in self.fieldMap ) {
if( self.fieldMap.hasOwnProperty( sourceFieldId ) && sourceFieldId == inputId ) {
break;
}
}
values[ sourceFieldId ? sourceFieldId : inputId ] = $( this ).val();
} );
return values;
}
self.doAllFieldsHaveValue = function() {
var values = Object.values( self.getAllValues() );
return values.length === values.filter( Boolean ).length;
}
self.doesValueExist = function( $elem ) {
if ( ! self.doAllFieldsHaveValue() ) {
self.removeIndicators();
return;
}
self.removeIndicators();
var spinner = new self.spinner( $elem, false, 'position:relative;top:2px;left:-25px;' ),
$buttons = $( '.gform_button' );
$buttons.prop( 'disabled', true );
self.$elems().prop( 'disabled', true );
$.post( self.ajaxUrl, {
nonce: self.nonce,
action: 'gwvev_does_value_exist',
values: self.getAllValues(),
form_id: self.sourceFormId
}, function( response ) {
self.$elems().prop( 'disabled', false );
$buttons.prop( 'disabled', false );
spinner.destroy();
if( ! response ) {
return;
}
response = $.parseJSON( response );
if( response.doesValueExist ) {
self.addIndicators( 'gwvev-response-success', '✔' );
} else {
self.addIndicators( 'gwvev-response-error', '✘' )
}
gform.doAction( 'gwvev_post_ajax_validation', self, response );
} );
};
self.$elems = function() {
return $( self.selectors.join( ', ' ) );
}
self.getIndicatorId = function( inputId ) {
return 'response_{0}_{1}'.gformFormat( self.targetFormId, inputId );
}
self.getIndicatorTemplate = function() {
return '<span id="{0}" class="gwvev-response {1}">{2}</span>';
}
self.removeIndicators = function() {
self.$elems().each( function() {
var inputId = gf_get_input_id_by_html_id( $( this ).attr( 'id' ) );
self.removeIndicator( inputId );
} );
}
self.removeIndicator = function( inputId ) {
$( '#' + self.getIndicatorId( inputId ) ).remove();
}
self.addIndicators = function( cssClass, icon ) {
self.$elems().each( function() {
var inputId = gf_get_input_id_by_html_id( $( this ).attr( 'id' ) );
self.addIndicator( $( this ), inputId, cssClass, icon );
} );
}
self.addIndicator = function( $elem, inputId, cssClass, icon ) {
$elem.after( self.getIndicatorTemplate().gformFormat( self.getIndicatorId( inputId ), cssClass, icon ) );
}
self.spinner = function( elem, imageSrc, inlineStyles ) {
imageSrc = typeof imageSrc == 'undefined' || ! imageSrc ? window.gf_global.spinnerUrl : imageSrc;
inlineStyles = typeof inlineStyles != 'undefined' ? inlineStyles : '';
this.elem = elem;
this.image = '<img class="gfspinner" src="' + imageSrc + '" style="' + inlineStyles + '" />';
this.init = function() {
this.spinner = jQuery(this.image);
jQuery(this.elem).after(this.spinner);
return this;
};
this.destroy = function() {
jQuery(this.spinner).remove();
};
return this.init();
};
self.init();
}
} )( jQuery );
</script>
<?php
self::$is_script_output = true;
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}
$args = array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'gwvev_does_value_exist' ),
'targetFormId' => $this->_args['target_form_id'],
'sourceFormId' => $this->_args['source_form_id'],
'selectors' => $this->get_selectors( $form ),
'fieldMap' => $this->get_field_map(),
'gfBaseUrl' => GFCommon::get_base_url(),
);
$script = 'new GWValueExistsValidation( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( 'gw_value_exists_validation', $this->_args['target_form_id'], $this->_args['target_field_id'] ) );
GFFormDisplay::add_init_script( $this->_args['target_form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function get_selectors( $form ) {
$selectors = array();
foreach ( $form['fields'] as $field ) {
if ( ! $this->is_applicable_field( $field ) ) {
continue;
}
$prefix = sprintf( '#input_%d_%d', $form['id'], $field->id );
if ( is_array( $field->inputs ) ) {
foreach ( $field->inputs as $input ) {
$bits = explode( '.', $input['id'] );
$input_id = $bits[1];
$selectors[] = "{$prefix}_{$input_id}";
}
} else {
$selectors[] = $prefix;
}
}
return $selectors;
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return $form_id == $this->_args['target_form_id'];
}
public function is_applicable_field( $field ) {
$field_map = $this->get_field_map();
return $this->is_applicable_form( $field->formId ) && in_array( $field->id, $field_map );
}
public function is_ajax_submission( $form_id, $is_ajax_enabled ) {
// Ensure GFFormDisplay is available before continuing to check (edge case with GPPA loading a pseudo form. See HS#25828)
return class_exists( 'GFFormDisplay' ) && isset( GFFormDisplay::$submission[ $form_id ] ) && $is_ajax_enabled;
}
}
# Configuration
new GW_Value_Exists_Validation( array(
'target_form_id' => 123,
'target_field_id' => 1,
'source_form_id' => 124,
'source_field_id' => 1,
'validation_message' => 'Hey! This isn\'t a valid reference number.',
) );
Hello, this creates a conflict with the signature of gravity forms. it is impossible to sign once this code is in place.
Hi,
I just tested the snippet with the signature field, but I wasn’t able to recreate the issue you’re experiencing. I’m guessing it could be specific to your setup. Please submit a ticket for this via our support form so we can dig into this further.
Best,