Fix conditions for multi-value fields
authorMatthias Schmidt <gravatronics@live.com>
Wed, 9 Oct 2019 17:04:02 +0000 (19:04 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Wed, 9 Oct 2019 17:04:02 +0000 (19:04 +0200)
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Abstract.js
wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/Dependency/Value.js
wcfsetup/install/files/lib/system/form/builder/field/dependency/ValueFormFieldDependency.class.php

index 180f9c68648476a2170dc3df66aaca246106ba47..62bb5e363e83125d459a1a8e0abd7ac59fd6126c 100644 (file)
@@ -76,7 +76,13 @@ define(['./Manager'], function(DependencyManager) {
                                }.bind(this));
                                
                                if (!this._fields.length) {
-                                       throw new Error("Unknown field with id '" + fieldId + "'.");
+                                       elBySelAll('input[type=checkbox][name="' + fieldId + '[]"]', undefined, function(field) {
+                                               this._fields.push(field);
+                                       }.bind(this));
+                                       
+                                       if (!this._fields.length) {
+                                               throw new Error("Unknown field with id '" + fieldId + "'.");
+                                       }
                                }
                        }
                        else {
index d39b880d49de5fa6ba537befb165cc754620b068..68f49d6d334e0b534aaf91550748bd82ad392fe9 100644 (file)
@@ -28,13 +28,13 @@ define(['./Abstract', 'Core', './Manager'], function(Abstract, Core, Manager) {
                                throw new Error("Values have not been set.");
                        }
                        
-                       var value;
+                       var values = [];
                        if (this._field) {
                                if (Manager.isHiddenByDependencies(this._field)) {
                                        return false;
                                }
                                
-                               value = this._field.value;
+                               values.push(this._field.value);
                        }
                        else {
                                for (var i = 0, length = this._fields.length, field; i < length; i++) {
@@ -45,21 +45,21 @@ define(['./Abstract', 'Core', './Manager'], function(Abstract, Core, Manager) {
                                                        return false;
                                                }
                                                
-                                               value = field.value;
-                                               
-                                               break;
+                                               values.push(field.value);
                                        }
                                }
                        }
                        
                        // do not use `Array.prototype.indexOf()` as we use a weak comparision
                        for (var i = 0, length = this._values.length; i < length; i++) {
-                               if (this._values[i] == value) {
-                                       if (this._isNegated) {
-                                               return false;
+                               for (var j = 0, length2 = values.length; j < length2; j++) {
+                                       if (this._values[i] == values[j]) {
+                                               if (this._isNegated) {
+                                                       return false;
+                                               }
+                                               
+                                               return true;
                                        }
-                                       
-                                       return true;
                                }
                        }
                        
index 9e11172d1ea4122b9775637b29f659e5f0a54250..57edd6d7b6dab3d524f484d0b8f65fabc8795444 100644 (file)
@@ -34,13 +34,27 @@ class ValueFormFieldDependency extends AbstractFormFieldDependency {
         * @inheritDoc
         */
        public function checkDependency() {
-               $inArray = in_array($this->getField()->getValue(), $this->getValues());
+               if (is_array($this->getField()->getValue())) {
+                       $check = false;
+                       // do not use `array_diff` because we use weak comparison
+                       foreach ($this->getValues() as $possibleValue) {
+                               foreach ($this->getField()->getValue() as $actualValue) {
+                                       if ($possibleValue == $actualValue) {
+                                               $check = true;
+                                               break;
+                                       }
+                               }
+                       }
+               }
+               else {
+                       $check = in_array($this->getField()->getValue(), $this->getValues());
+               }
                
                if ($this->isNegated()) {
-                       return !$inArray;
+                       return !$check;
                }
                
-               return $inArray;
+               return $check;
        }
        
        /**