[BUGFIX] group field in TCEforms does not honor size=1 attribute
authorDmitry Dulepov <dmitry@typo3.org>
Tue, 28 Jun 2011 08:58:33 +0000 (11:58 +0300)
committerJigal van Hemert <jigal@xs4all.nl>
Sun, 31 Jul 2011 22:17:25 +0000 (00:17 +0200)
If the field has size=1, it should be shown as a "single entry"
selector box. In all cases it has a 'multiple="multiple"'
attribute hard-coded, which prevents the control from being
single line. The solution is not to add that attribute unless
necessary.

Change-Id: Iab244a0b797f117d270714ca14d88236399ead93
Resolves: #25221
Related: #24256
Reviewed-on: http://review.typo3.org/972
Reviewed-by: Georg Ringer
Reviewed-by: Stefan Neufeind
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Jigal van Hemert
Tested-by: Jigal van Hemert
t3lib/class.t3lib_tceforms.php

index 4493abf..eae1885 100644 (file)
@@ -3568,7 +3568,8 @@ class t3lib_TCEforms {
                        // Create selector box of the options
                $sSize = $params['autoSizeMax'] ? t3lib_utility_Math::forceIntegerInRange($itemArrayC + 1, t3lib_utility_Math::forceIntegerInRange($params['size'], 1), $params['autoSizeMax']) : $params['size'];
                if (!$selector) {
-                       $selector = '<select id="' . uniqid('tceforms-multiselect-') . '" ' . ($params['noList'] ? 'style="display: none"' : 'size="' . $sSize . '"' . $this->insertDefStyle('group', 'tceforms-multiselect')) . ' multiple="multiple" name="' . $fName . '_list" ' . $onFocus . $params['style'] . $disabled . '>' . implode('', $opt) . '</select>';
+                       $isMultiple = ($params['size'] != 1 && $params['multiple']);
+                       $selector = '<select id="' . uniqid('tceforms-multiselect-') . '" ' . ($params['noList'] ? 'style="display: none"' : 'size="' . $sSize . '"' . $this->insertDefStyle('group', 'tceforms-multiselect')) . ($isMultiple ? ' multiple="multiple"' : '') . ' name="' . $fName . '_list" ' . $onFocus . $params['style'] . $disabled . '>' . implode('', $opt) . '</select>';
                }
 
 
@@ -5570,18 +5571,17 @@ class t3lib_TCEforms {
                                browserWin.focus();
                        }
                        function setFormValueFromBrowseWin(fName,value,label,exclusiveValues) {
-                               var formObj = setFormValue_getFObj(fName);
+                               var formObj = setFormValue_getFObj(fName), fObj, isMultiple = false, isList = false, len;
                                if (formObj && value !== "--div--") {
                                                // Check if the form object has a "_list" element or not
                                                // The "_list" element exists for multiple selection select types
-                                       var isMultiple = true;
                                        if (formObj[fName + "_list"]) {
                                                fObj = formObj[fName + "_list"];
+                                               isMultiple = fObj.getAttribute("multiple") == null && fObj.getAttribute("size") != "1";
+                                               isList = true;
                                        } else {
                                                fObj = formObj[fName];
-                                               var isMultiple = false;
                                        }
-                                       var len = fObj.length;
 
                                                // clear field before adding value, if configured so (maxitems==1)
                                        if (typeof TBE_EDITOR.clearBeforeSettingFormValueFromBrowseWin[fName] != "undefined") {
@@ -5598,14 +5598,21 @@ class t3lib_TCEforms {
                                                len = fObj.length;
                                        }
 
-                                       if (isMultiple) {
+                                       if (isMultiple || isList) {
+                                               if (!isMultiple) {
+                                                               // If multiple values are not allowed, clear anything that is in the control already
+                                                       fObj.options.length = 0;
+                                                       fObj.length = 0; // Note: this is dangerous! "length" on the object is a reserved JS attribute!
+                                               }
+                                               len = fObj.length;
+
                                                        // Clear elements if exclusive values are found
                                                if (exclusiveValues) {
                                                        var m = new RegExp("(^|,)" + value + "($|,)");
                                                        if (exclusiveValues.match(m))   {
                                                                        // the new value is exclusive
                                                                for (a = len - 1; a >= 0; a--) {
-                                                                       fObj[a] = null;
+                                                                       fObj[a] = null; // This is dangerous because it works on the object\'s numeric properties directly instead of using a custom attribute!
                                                                }
                                                                len = 0;
                                                        } else if (len == 1)    {
@@ -5618,11 +5625,11 @@ class t3lib_TCEforms {
                                                        }
                                                }
                                                        // Inserting element
-                                               var setOK = 1;
+                                               var setOK = true;
                                                if (!formObj[fName + "_mul"] || formObj[fName + "_mul"].value == 0) {
                                                        for (a = 0; a < len; a++) {
                                                                if (fObj.options[a].value == value) {
-                                                                       setOK = 0;
+                                                                       setOK = false;
                                                                }
                                                        }
                                                }
@@ -5809,7 +5816,12 @@ class t3lib_TCEforms {
                                var formObj = ' . $formObj . ';
                                if (formObj) {
                                                // Take the form object if it is either of type select-one or of type-multiple and it has a "_list" element
-                                       if (formObj[fName] && ((formObj[fName].type == "select-one") || (formObj[fName + "_list"] && formObj[fName + "_list"].type == "select-multiple"))) {
+                                       if (formObj[fName] &&
+                                               (
+                                                       (formObj[fName].type == "select-one") ||
+                                                       (formObj[fName + "_list"] && formObj[fName + "_list"].type.match(/select-(one|multiple)/))
+                                               )
+                                       ) {
                                                return formObj;
                                        } else {
                                                alert("Formfields missing:\n fName: " + formObj[fName] + "\n fName_list:" + formObj[fName + "_list"] + "\n type:" + formObj[fName + "_list"].type + "\n fName:" + fName);