Field to Field Conditional Logic
Compare fields in Gravity Forms conditional logic.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gw-field-to-field-conditional-logic.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
<?php
/**
* Gravity Wiz // Gravity Forms // Field to Field Conditional Logic
*
* Compare fields in Gravity Forms conditional logic. Is Field A greater than Field B? Is the emergency contact's name different than
* the patient's? Is Date A after Date B?
*
* Plugin Name: GF Field to Field Conditional Logic
* Plugin URI: https://gravitywiz.com/
* Description: Compare fields in Gravity Forms conditional logic.
* Author: Gravity Wiz
* Version: 0.12
* Author URI: https://gravitywiz.com
*
* @todo
* - Add UI for selecting merge tags.
* - Add support for CL on next/prev buttons.
*/
class GF_Field_To_Field_Conditional_Logic {
public function __construct() {
// 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() {
add_filter( 'gform_admin_pre_render', array( $this, 'enqueue_inline_admin_script' ) );
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
add_filter( 'gform_rule_pre_evaluation', array( $this, 'modify_rule' ), 10, 5 );
}
public function enqueue_inline_admin_script( $return ) {
add_filter( 'admin_footer', array( $this, 'output_admin_inline_script' ) );
return $return;
}
public function output_admin_inline_script() {
?>
<script>
gform.addFilter( 'gform_conditional_logic_values_input', function( markup, objectType, ruleIndex, selectedFieldId, selectedValue ) {
var selectedField = GetFieldById( selectedFieldId );
if ( ! selectedField || ! selectedField.choices.length ) {
return markup;
}
// If this is not a <select> return the markup unmodified.
var match = markup.match( /(<select.+?>)((?:.|\n)+?)(<\/select>)/ );
if ( ! match ) {
return markup;
}
var choiceOptions = match ? match[2] : markup;
var fieldOptions = [];
for ( var field of window.form.fields ) {
if ( ! IsConditionalLogicField( field ) ) {
continue;
}
var value = '{:' + field.id + ':value}';
var isSelected = value === selectedValue;
fieldOptions.push( '<option value="{0}" {2}>{1}</option>'.gformFormat( value, GetLabel( field ), isSelected ? 'selected' : '' ) );
if ( isSelected ) {
var $choiceSelect = jQuery( '<select>' + choiceOptions + '</select>' );
$choiceSelect.find( 'option:selected' ).remove();
choiceOptions = $choiceSelect.html();
$choiceSelect.remove();
}
}
markup = '{0}<optgroup label="Field Choices">{1}</optgroup><optgroup label="Fields">{2}</optgroup>{3}'.gformFormat(
match ? match[1] : '',
choiceOptions,
fieldOptions.join( "\n" ),
match ? match[3] : '',
);
return markup;
}, 9 );
</script>
<?php
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
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.GWFieldToFieldConditionalLogic = function( args ) {
var self = this;
// copy all args to current object: (list expected props)
for( var prop in args ) {
if( args.hasOwnProperty( prop ) ) {
self[ prop ] = args[ prop ];
}
}
self.init = function() {
// Let GF know that when are target field's value changes, we need to re-evaluate conditional logic.
for ( var prop in self.dependencies ) {
if( self.dependencies.hasOwnProperty( prop ) ) {
if ( typeof window.gf_form_conditional_logic[ self.formId ].fields[ prop ] === 'undefined' ) {
window.gf_form_conditional_logic[ self.formId ].fields[ prop ] = [];
}
window.gf_form_conditional_logic[ self.formId ].fields[ prop ] = window.gf_form_conditional_logic[ self.formId ].fields[ prop ].concat( self.dependencies[ prop ] );
}
}
// Replace the field merge tag in the rule value before the rule is evaluated.
gform.addFilter( 'gform_rule_pre_evaluation', function( rule, formId ) {
var mergeTags = GFMergeTag.parseMergeTags( rule.value );
if ( ! mergeTags.length ) {
return rule;
}
rule.value = GFMergeTag.getMergeTagValue( formId, mergeTags[0][1], mergeTags[0][3] );
var fieldNumberFormat = gf_get_field_number_format( rule.fieldId, formId, 'value' );
if( fieldNumberFormat ) {
rule.value = gf_format_number( rule.value, fieldNumberFormat );
}
return rule;
} );
};
self.init();
}
} )( jQuery );
</script>
<?php
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}
$args = array(
'formId' => $form['id'],
'dependencies' => $this->get_cl_dependencies( $form ),
);
$script = 'new GWFieldToFieldConditionalLogic( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( 'gw_field_to_field_conditional_logic', $form['id'] ) );
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function get_cl_dependencies( $object, $dependencies = array() ) {
foreach ( $object as $prop => $value ) {
if ( $prop === 'conditionalLogic' && ! empty( $value ) ) {
foreach ( $object[ $prop ]['rules'] as $rule ) {
$matches = $this->parse_merge_tags( $rule['value'] );
if ( ! empty( $matches ) ) {
$tag_field_id = $matches[0][1];
if ( ! isset( $dependencies[ $tag_field_id ] ) ) {
$dependencies[ $tag_field_id ] = array();
}
// Submit button conditional logic is 0.
array_push( $dependencies[ $tag_field_id ], rgar( $object, 'id', 0 ) );
}
}
} elseif ( is_array( $value ) || is_a( $value, 'GF_Field' ) ) {
$dependencies = $this->get_cl_dependencies( $value, $dependencies );
}
}
return $dependencies;
}
public function parse_merge_tags( $string, $pattern = '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi' ) {
preg_match_all( $pattern, $string, $matches, PREG_SET_ORDER );
return $matches;
}
public function is_applicable_form( $form ) {
// @todo we will need to recursively search for field-to-field conditional logic (see GPCLD).
return true;
}
/**
* Parse merge tags in rule values on submission (and other times conditional logic is evaluated).
*
* @param $rule
* @param $form
* @param $logic
* @param $field_values
* @param $entry
*
* @return mixed
*/
public function modify_rule( $rule, $form, $logic, $field_values, $entry ) {
static $_current_entry;
static $_is_modifying_rule;
static $_rule_cache;
if ( $_is_modifying_rule ) {
return $rule;
}
if ( $entry === null ) {
if ( ! $_current_entry ) {
$_is_modifying_rule = true;
$_current_entry = GFFormsModel::get_current_lead();
$_is_modifying_rule = false;
/*
* Clear the GF visibility cache for this form. When we get the current lead, the visibility is cached
* without the logic from this snippet.
*
* This then caches some fields into a hidden state which causes them to not be validated.
*/
if ( ! empty( $form['fields'] ) && is_array( $form['fields'] ) ) {
foreach ( $form['fields'] as $field ) {
GFCache::delete( 'GFFormsModel::is_field_hidden_' . $form['id'] . '_' . $field->id );
}
}
}
$entry = $_current_entry;
}
$entry_id = rgar( $entry, 'id' );
if ( ! isset( $_rule_cache[ $entry_id ] ) ) {
$_rule_cache[ $entry_id ] = array();
}
if ( isset( $_rule_cache[ $entry_id ][ $rule['value'] ] ) ) {
$value = $_rule_cache[ $entry_id ][ $rule['value'] ];
} else {
$value = GFCommon::replace_variables( $rule['value'], $form, $entry );
$_rule_cache[ $entry_id ][ $rule['value'] ] = $value;
}
// Clean number values to ensure proper comparison.
if ( GFCommon::is_numeric( $value ) ) {
$value = GFCommon::clean_number( $value );
}
$rule['value'] = $value;
return $rule;
}
}
# Configuration
new GF_Field_To_Field_Conditional_Logic();
This is exactly what I’ve been looking for, but I need help to implement. Cannot figure out where to sub in variables and where adjust conditional logic.
FWIW, my immediate need it this. I have a Unique ID field that gets populated on submission. This field is named “JobNumber”. I need to know when the form is being filled out for the first time (a “New Entry”), as opposed to when it is being edited in Gravity View (“Edit Entry”). The easiest way I’ve found to do this is to test the “JobNumber” field for “>0”. Now, I need to populate a check box or radio button field (“JobEntryStatus”) as either “New Entry” or “Edit Entry” based on the outcome of that test.
Hi there,
I think it would be best for us to take a closer look at your form here so we have a full understanding of how it all fits together. I’ve sent you a follow-up email so we can dig in some more.
Best,
So this seems to only be working when you’re using the for while logged in. Is there a way to make it work when someone is not logged in or am I missing something :/
Hi Trey,
The snippet should work even not logged in. However, if the field you’re using for the conditional logic depends on whether the user is logged in, then that could be the reason. We’ll contact you via email so we can have a closer look at your setup.
Best,
This is exactly what I need, but how do you use it?
Hi Jonathon,
Once the snippet is installed, you can add the merge tag for the field you want to compare against to the conditional logic statement. I see that you opened a support ticket for this issue, too. I’ve replied to you there with additional detail.
Best,