[BUGFIX] Correctly handle validation of multi-selects in FormEngine 37/45437/2
authorMarkus Klein <markus.klein@typo3.org>
Mon, 21 Dec 2015 15:49:37 +0000 (16:49 +0100)
committerAndreas Fernandez <typo3@scripting-base.de>
Thu, 24 Dec 2015 13:20:45 +0000 (14:20 +0100)
Resolves: #72366
Releases: master, 7.6
Change-Id: I7627a2a5b1aec4428d160e6b201373b8cef779ee
Reviewed-on: https://review.typo3.org/45437
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
typo3/sysext/backend/Resources/Public/JavaScript/FormEngineValidation.js

index 9cbb5af..f967f26 100644 (file)
@@ -235,7 +235,10 @@ define(['jquery', 'TYPO3/CMS/Backend/FormEngine'], function ($, FormEngine) {
         * @returns {String}
         */
        FormEngineValidation.validateField = function($field, value) {
-               value = value || FormEngineValidation.ltrim($field.val());
+               value = value || $field.val();
+               if (!$.isArray(value)) {
+                       value = FormEngineValidation.ltrim(value);
+               }
 
                var rules = $field.data('formengine-validation-rules');
                var markParent = false;
@@ -473,8 +476,25 @@ define(['jquery', 'TYPO3/CMS/Backend/FormEngine'], function ($, FormEngine) {
                $(FormEngineValidation.rulesSelector).each(function() {
                        var $field = $(this);
                        if (!$field.closest('.t3js-flex-section-deleted, .t3js-inline-record-deleted').length) {
-                               var newValue = FormEngineValidation.validateField($field);
-                               if (newValue.length && $field.val() !== newValue) {
+                               var modified = false;
+                               var currentValue = $field.val();
+                               var newValue = FormEngineValidation.validateField($field, currentValue);
+                               if ($.isArray(newValue) && $.isArray(currentValue)) {
+                                       // handling for multi-selects
+                                       if (newValue.length !== currentValue.length) {
+                                               modified = true;
+                                       } else {
+                                               for (var i = 0; i < newValue.length; i++) {
+                                                       if (newValue[i] !== currentValue[i]) {
+                                                               modified = true;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                               } else if (newValue.length && currentValue !== newValue) {
+                                       modified = true;
+                               }
+                               if (modified) {
                                        $field.attr('value', newValue);
                                        FormEngineValidation.setCaretPosition($field, 0);
                                }