[BUGFIX] Treat numbers for comparison in FormEngineValidation as integer 24/47424/3
authorAndreas Fernandez <a.fernandez@scripting-base.de>
Wed, 30 Mar 2016 09:15:44 +0000 (11:15 +0200)
committerAndreas Fernandez <typo3@scripting-base.de>
Wed, 30 Mar 2016 13:27:57 +0000 (15:27 +0200)
This patch fixes the issue that the number-based rules are treated
as strings. The numbers are converted to integers now.

Also, the ``range`` is changed: Instead of defining any fallback
values if either ``lower`` or ``upper`` are not set, the value is now
only compared against those limits when they exist.

Resolves: #75307
Releases: master, 7.6
Change-Id: Iab321a7637e2ccc8c662355ccef1e25bc0b78be7
Reviewed-on: https://review.typo3.org/47424
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 f967f26..a53b4cc 100644 (file)
@@ -245,6 +245,8 @@ define(['jquery', 'TYPO3/CMS/Backend/FormEngine'], function ($, FormEngine) {
                var selected = 0;
                var returnValue = value;
                var $relatedField;
+               var minItems;
+               var maxItems;
                $.each(rules, function(k, rule) {
                        switch (rule.type) {
                                case 'required':
@@ -259,22 +261,32 @@ define(['jquery', 'TYPO3/CMS/Backend/FormEngine'], function ($, FormEngine) {
                                                        $relatedField = $(document).find('[name="' + $field.data('relatedfieldname') + '"]');
                                                        if ($relatedField.length) {
                                                                selected = FormEngineValidation.trimExplode(',', $relatedField.val()).length;
-                                                               if (selected < rule.minItems || selected > rule.maxItems) {
-                                                                       markParent = true;
-                                                                       $field.closest(FormEngineValidation.markerSelector).addClass(FormEngineValidation.errorClass);
-                                                               }
                                                        } else {
                                                                selected = $field.val();
-                                                               if (selected < rule.minItems || selected > rule.maxItems) {
+                                                       }
+                                                       if (typeof rule.minItems !== 'undefined') {
+                                                               minItems = rule.minItems * 1;
+                                                               if (!isNaN(minItems) && selected < minItems) {
+                                                                       markParent = true;
+                                                               }
+                                                       }
+                                                       if (typeof rule.maxItems !== 'undefined') {
+                                                               maxItems = rule.maxItems * 1;
+                                                               if (!isNaN(maxItems) && selected > maxItems) {
                                                                        markParent = true;
                                                                        $field.closest(FormEngineValidation.markerSelector).addClass(FormEngineValidation.errorClass);
                                                                }
                                                        }
                                                }
-                                               if (rule.config.lower || rule.config.upper) {
-                                                       var minValue = rule.config.lower || 0;
-                                                       var maxValue = rule.config.upper || Number.MAX_VALUE;
-                                                       if (value < minValue || value > maxValue) {
+                                               if (typeof rule.config.lower !== 'undefined') {
+                                                       var minValue = rule.config.lower * 1;
+                                                       if (!isNaN(minValue) && value < minValue) {
+                                                               markParent = true;
+                                                       }
+                                               }
+                                               if (typeof rule.config.upper !== 'undefined') {
+                                                       var maxValue = rule.config.upper * 1;
+                                                       if (!isNaN(maxValue) && value > maxValue) {
                                                                markParent = true;
                                                                $field.closest(FormEngineValidation.markerSelector).addClass(FormEngineValidation.errorClass);
                                                        }
@@ -286,13 +298,18 @@ define(['jquery', 'TYPO3/CMS/Backend/FormEngine'], function ($, FormEngine) {
                                                $relatedField = $(document).find('[name="' + $field.data('relatedfieldname') + '"]');
                                                if ($relatedField.length) {
                                                        selected = FormEngineValidation.trimExplode(',', $relatedField.val()).length;
-                                                       if (selected < rule.minItems || selected > rule.maxItems) {
-                                                               markParent = true;
-                                                               $field.closest(FormEngineValidation.markerSelector).addClass(FormEngineValidation.errorClass);
-                                                       }
                                                } else {
                                                        selected = $field.find('option:selected').length;
-                                                       if (selected < rule.minItems || selected > rule.maxItems) {
+                                               }
+                                               if (typeof rule.minItems !== 'undefined') {
+                                                       minItems = rule.minItems * 1;
+                                                       if (!isNaN(minItems) && selected < minItems) {
+                                                               markParent = true;
+                                                       }
+                                               }
+                                               if (typeof rule.maxItems !== 'undefined') {
+                                                       maxItems = rule.maxItems * 1;
+                                                       if (!isNaN(maxItems) && selected > maxItems) {
                                                                markParent = true;
                                                                $field.closest(FormEngineValidation.markerSelector).addClass(FormEngineValidation.errorClass);
                                                        }
@@ -302,18 +319,34 @@ define(['jquery', 'TYPO3/CMS/Backend/FormEngine'], function ($, FormEngine) {
                                case 'group':
                                        if (rule.minItems || rule.maxItems) {
                                                selected = $field.find('option').length;
-                                               if (selected < rule.minItems || selected > rule.maxItems) {
-                                                       markParent = true;
-                                                       $field.closest(FormEngineValidation.markerSelector).addClass(FormEngineValidation.errorClass);
+                                               if (typeof rule.minItems !== 'undefined') {
+                                                       minItems = rule.minItems * 1;
+                                                       if (!isNaN(minItems) && selected < minItems) {
+                                                               markParent = true;
+                                                       }
+                                               }
+                                               if (typeof rule.maxItems !== 'undefined') {
+                                                       maxItems = rule.maxItems * 1;
+                                                       if (!isNaN(maxItems) && selected > maxItems) {
+                                                               markParent = true;
+                                                       }
                                                }
                                        }
                                        break;
                                case 'inline':
                                        if (rule.minItems || rule.maxItems) {
                                                selected = FormEngineValidation.trimExplode(',', $field.val()).length;
-                                               if (selected < rule.minItems || selected > rule.maxItems) {
-                                                       markParent = true;
-                                                       $field.closest(FormEngineValidation.markerSelector).addClass(FormEngineValidation.errorClass);
+                                               if (typeof rule.minItems !== 'undefined') {
+                                                       minItems = rule.minItems * 1;
+                                                       if (!isNaN(minItems) && selected < minItems) {
+                                                               markParent = true;
+                                                       }
+                                               }
+                                               if (typeof rule.maxItems !== 'undefined') {
+                                                       maxItems = rule.maxItems * 1;
+                                                       if (!isNaN(maxItems) && selected > maxItems) {
+                                                               markParent = true;
+                                                       }
                                                }
                                        }
                                        break;