|
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,9 +42,21 @@ 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 | } );
|
| 53 | + |
| 54 | + let origRuleNeedsTextValue = window.ruleNeedsTextValue; |
| 55 | + // Override the default GF function to add our custom operator. |
| 56 | + window.ruleNeedsTextValue = function( rule ) { |
| 57 | + let needsTextValue = origRuleNeedsTextValue( rule ); |
| 58 | + return needsTextValue || rule.operator.indexOf( 'does_not_contain' ) !== -1; |
| 59 | + } |
48 | 60 | </script>
|
49 | 61 | <?php
|
50 | 62 | }
|
@@ -82,18 +94,21 @@ public function output_script() {
|
82 | 94 | }
|
83 | 95 |
|
84 | 96 | var fieldValue = '';
|
85 |
| - var $field = $( '#input_' + formId + '_' + rule.fieldId ); |
86 |
| - |
87 |
| - // Handle different field types |
88 |
| - if ( $field.is(':checkbox') || $field.is(':radio') ) { |
89 |
| - fieldValue = $field.filter(':checked').map(function() { |
90 |
| - 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; |
91 | 105 | }).get().join(',');
|
92 |
| - } else if ( $field.is('select[multiple]') ) { |
93 |
| - fieldValue = $field.val() ? $field.val().join(',') : ''; |
| 106 | + } else if ( $inputs.is('select[multiple]') ) { |
| 107 | + fieldValue = $inputs.val() ? $inputs.val().join(',') : ''; |
94 | 108 | } else {
|
95 | 109 | fieldValue = $field.val() || '';
|
96 | 110 | }
|
| 111 | + |
97 | 112 | isMatch = typeof fieldValue === 'string' && fieldValue.indexOf( rule.value ) === -1;
|
98 | 113 |
|
99 | 114 | return isMatch;
|
|
0 commit comments