Conditional Logic Operator: “Does Not Contain”
Adds support for the “does not contain” conditional logic operator in Gravity Forms.
Instructions
Code
Filename: gw-conditional-logic-operator-does-not-contain.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
<?php
/**
* Gravity Wiz // Gravity Forms // Conditional Logic Operator: "Does Not Contain"
*
* Instruction Video: https://www.loom.com/share/8e1b27ec47b341dbb4f0da2bec6a960b
*
* Check if a source field value does NOT contain a specific substring using the "does not contain" conditional logic operator.
*
* Plugin Name: GF Conditional Logic Operator: "Does Not Contain"
* Plugin URI: https://gravitywiz.com/
* Description: Adds support for the "does not contain" conditional logic operator in Gravity Forms.
* Author: Gravity Wiz
* Version: 1.0
* Author URI: https://gravitywiz.com
*/
class GF_CLO_Does_Not_Contain {
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_filter( 'admin_footer', array( $this, 'output_admin_inline_script' ) );
add_filter( 'gform_is_valid_conditional_logic_operator', array( $this, 'whitelist_operator' ), 10, 2 );
add_filter( 'gpeu_field_filters_from_conditional_logic', array( $this, 'convert_conditional_logic_to_field_filters' ) );
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_is_value_match', array( $this, 'evaluate_operator' ), 10, 6 );
}
public function output_admin_inline_script() {
if ( ! GFForms::get_page() && is_admin() && ! in_array( rgget( 'page' ), array( 'gp-email-users' ) ) ) {
return;
}
?>
<script>
if ( window.gf_vars ) {
gf_vars['does_not_contain'] = '<?php esc_html_e( 'does not contain' ); ?>';
}
gform.addFilter( 'gform_conditional_logic_operators', function( operators ) {
// Injects our "does_not_contain" operator directly below the "contains" operator for logical ordering.
operators = Object.fromEntries(
Object.entries(operators).flatMap(([k, v]) =>
k === "contains" ? [[k, v], ['does_not_contain', 'does not contain']] : [[k, v]]
)
);
return operators;
} );
let origRuleNeedsTextValue = window.ruleNeedsTextValue;
// Override the default GF function to add our custom operator.
window.ruleNeedsTextValue = function( rule ) {
let needsTextValue = origRuleNeedsTextValue( rule );
return needsTextValue || rule.operator.indexOf( 'does_not_contain' ) !== -1;
}
</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.GWCLODoesNotContain = function( args ) {
var self = this;
for ( var prop in args ) {
if ( args.hasOwnProperty( prop ) ) {
self[ prop ] = args[ prop ];
}
}
self.init = function() {
gform.addFilter( 'gform_is_value_match', function( isMatch, formId, rule ) {
if ( rule.operator !== 'does_not_contain' ) {
return isMatch;
}
var fieldValue = '';
var $field = $( '#input_' + formId + '_' + rule.fieldId );
var $inputs = $field.find( 'input, select, textarea' );
// This is a quick-and-dirty way to get the value of the field. We may need to revisit for
// edge cases in the future.
if ( $inputs.is(':checkbox') || $inputs.is(':radio') ) {
fieldValue = $inputs.filter(':checked').map(function() {
return this.value;
}).get().join(',');
} else if ( $inputs.is('select[multiple]') ) {
fieldValue = $inputs.val() ? $inputs.val().join(',') : '';
} else {
fieldValue = $field.val() || '';
}
isMatch = typeof fieldValue === 'string' && fieldValue.indexOf( rule.value ) === -1;
return isMatch;
} );
};
self.init();
}
} )( jQuery );
</script>
<?php
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}
$script = 'new GWCLODoesNotContain();';
$slug = implode( '_', array( 'gwclo_does_not_contain', $form['id'] ) );
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function whitelist_operator( $is_valid, $operator ) {
if ( $operator === 'does_not_contain' ) {
$is_valid = true;
}
return $is_valid;
}
public function evaluate_operator( $is_match, $field_value, $target_value, $operation, $source_field, $rule ) {
if ( $rule['operator'] !== 'does_not_contain' || rgar( $rule, 'gwclodncEvaluatingOperator' ) ) {
return $is_match;
}
$rule['gwclodncEvaluatingOperator'] = true;
// If the field contains the target value, it's not a match.
$is_match = strpos( $field_value, $target_value ) === false;
$rule['gwclodncEvaluatingOperator'] = false;
return $is_match;
}
public function convert_conditional_logic_to_field_filters( $field_filters ) {
foreach ( $field_filters as &$field_filter ) {
if ( ! is_array( $field_filter ) ) {
continue;
}
switch ( $field_filter['operator'] ) {
case 'does_not_contain':
$field_filter['operator'] = 'NOT LIKE';
$field_filter['value'] = '%' . $field_filter['value'] . '%';
break;
}
}
return $field_filters;
}
public function is_applicable_form( $form ) {
return GFFormDisplay::has_conditional_logic( $form );
}
}
# Configuration
new GF_CLO_Does_Not_Contain();
Awesome. My sys admin has been dreaming of this for years.
Hey Christopher,
Ah, that’s awesome to hear!
Cheers,
Great! Also helpful would be ≥, ≤, not >, not <. For instance, a conditional test for not > 0 would work for a 0 entry and a blank entry.
Hey Peter,
Thanks for the suggestion!
For that specific example, if you are looking to check if the value of a Single Line Text field is 0 OR blank, you could currently accomplish this by setting up two separate conditions, and then checking if ANY of those conditions are true.
Do you have any other specific examples in mind? We’d be happy to explore some of these further via support.
Why this is not part of the Gravity Forms core plugin I will never know. Great addition!
Hey Aaron,
Glad to hear that you’re enjoying it