Conditional Logic broken

Open Beginner friendly
#548 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
javascript
Domain
frontend

Research direction

Start in assets/build/js/acf-input.js at the HasValue and LessThan conditional logic handlers. Reproduce the reported cases with a multi-select user field and a cloned field, then verify that empty arrays and null, undefined, or false values are handled as described. Done means both conditional logic cases work without the reported workarounds.

Written by the indexing model from the issue text.

Description

As explained in the support ticket #1619769049, conditional logic has broken either in a WP upgrade between 5.5.5 and 5.8 or an ACF upgrade between 5.9.6 and 5.9.9.

The HasValue condition does no longer work on multi-select user field. The work-around using a LessThan rule does not work on cloned fields.

The reason for the first error is, that an empty Array is considered to be a non-empty value.
The reason for the second error is that isLessThan() consideres null to be not-less-than-one

Both can be fixed with the following patch:

Index: assets/build/js/acf-input.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/assets/build/js/acf-input.js b/assets/build/js/acf-input.js
--- a/assets/build/js/acf-input.js	(revision 8fd2564c86de244ca0b88c7773e0a861d7044cc5)
+++ b/assets/build/js/acf-input.js	(date 1630616522694)
@@ -5289,7 +5289,11 @@
 		label: __('Has any value'),
 		fieldTypes: [ 'text', 'textarea', 'number', 'range', 'email', 'url', 'password', 'image', 'file', 'wysiwyg', 'oembed', 'select', 'checkbox', 'radio', 'button_group', 'link', 'post_object', 'page_link', 'relationship', 'taxonomy', 'user', 'google_map', 'date_picker', 'date_time_picker', 'time_picker', 'color_picker' ],
 		match: function( rule, field ){
-			return (field.val() ? true : false);
+			var val = field.val();
+			if( val instanceof Array ) {
+				val = val.length;
+			}
+			return (val ? true : false);
 		},
 		choices: function( fieldObject ){
 			return '<input type="text" disabled="" />';
@@ -5625,6 +5629,9 @@
 			if( val instanceof Array ) {
 				val = val.length;
 			}
+			if( val === undefined || val === null || val === false ) {
+				return true;
+			}
 			return isLessThan( val, rule.value );
 		},
 		choices: function( fieldObject ){
Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AdvancedCustomFields/acf

All issues in AdvancedCustomFields/acf

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.