|
1 | 1 | <?php |
2 | 2 | /** |
3 | 3 | * Gravity Wiz // Gravity Forms // Conditional Logic Operator: "Does Not Contain" |
4 | | - * |
| 4 | + * |
5 | 5 | * Instruction Video: https://www.loom.com/share/8e1b27ec47b341dbb4f0da2bec6a960b |
6 | 6 | * |
7 | 7 | * Check if a source field value does NOT contain a specific substring using the "does not contain" conditional logic operator. |
@@ -42,13 +42,18 @@ public function output_admin_inline_script() { |
42 | 42 | } |
43 | 43 |
|
44 | 44 | gform.addFilter( 'gform_conditional_logic_operators', function( operators ) { |
45 | | - operators.does_not_contain = 'does not contain'; |
| 45 | + // Injects our "does_not_contain" operator directly below the "contains" operator for logical ordering. |
| 46 | + operators = Object.fromEntries( |
| 47 | + Object.entries(operators).flatMap(([k, v]) => |
| 48 | + k === "contains" ? [[k, v], ['does_not_contain', 'does not contain']] : [[k, v]] |
| 49 | + ) |
| 50 | + ); |
46 | 51 | return operators; |
47 | 52 | } ); |
48 | 53 |
|
49 | 54 | let origRuleNeedsTextValue = window.ruleNeedsTextValue; |
50 | 55 | // Override the default GF function to add our custom operator. |
51 | | - function ruleNeedsTextValue( rule ) { |
| 56 | + window.ruleNeedsTextValue = function( rule ) { |
52 | 57 | let needsTextValue = origRuleNeedsTextValue( rule ); |
53 | 58 | return needsTextValue || rule.operator.indexOf( 'does_not_contain' ) !== -1; |
54 | 59 | } |
@@ -89,18 +94,21 @@ public function output_script() { |
89 | 94 | } |
90 | 95 |
|
91 | 96 | var fieldValue = ''; |
92 | | - var $field = $( '#input_' + formId + '_' + rule.fieldId ); |
93 | | - |
94 | | - // Handle different field types |
95 | | - if ( $field.is(':checkbox') || $field.is(':radio') ) { |
96 | | - fieldValue = $field.filter(':checked').map(function() { |
97 | | - return this.value; |
| 97 | + var $field = $( '#input_' + formId + '_' + rule.fieldId ); |
| 98 | + var $inputs = $field.find( 'input, select, textarea' ); |
| 99 | + |
| 100 | + // This is a quick-and-dirty way to get the value of the field. We may need to revisit for |
| 101 | + // edge cases in the future. |
| 102 | + if ( $inputs.is(':checkbox') || $inputs.is(':radio') ) { |
| 103 | + fieldValue = $inputs.filter(':checked').map(function() { |
| 104 | + return this.value; |
98 | 105 | }).get().join(','); |
99 | | - } else if ( $field.is('select[multiple]') ) { |
100 | | - fieldValue = $field.val() ? $field.val().join(',') : ''; |
| 106 | + } else if ( $inputs.is('select[multiple]') ) { |
| 107 | + fieldValue = $inputs.val() ? $inputs.val().join(',') : ''; |
101 | 108 | } else { |
102 | 109 | fieldValue = $field.val() || ''; |
103 | 110 | } |
| 111 | + |
104 | 112 | isMatch = typeof fieldValue === 'string' && fieldValue.indexOf( rule.value ) === -1; |
105 | 113 |
|
106 | 114 | return isMatch; |
|
0 commit comments