[TASK] Optimize FormEngineValidation, Part II 92/48192/8
authorAndreas Fernandez <a.fernandez@scripting-base.de>
Wed, 30 Mar 2016 13:12:08 +0000 (15:12 +0200)
committerJan Helke <typo3@helke.de>
Thu, 23 Nov 2017 08:13:25 +0000 (09:13 +0100)
Some functions inside FormEngineValidation are optimized to reduce
runtime or run multiple validations on fields that are already
marked as "failed".

Resolves: #81053
Releases: master
Change-Id: I751d5e99df2b58c14f629c84b305ec49bcc0d411
Reviewed-on: https://review.typo3.org/48192
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
typo3/sysext/backend/Resources/Public/JavaScript/FormEngineValidation.js

index 628fe04..999cee0 100644 (file)
@@ -148,7 +148,7 @@ define(['jquery', 'moment'], function ($, moment) {
                                                theString = date.format('DD-MM-YYYY');
                                        }
                                } else {
-                                       parsedInt = parseInt(value);
+                                       parsedInt = value * 1;
                                        if (!parsedInt) {
                                                return '';
                                        }
@@ -250,6 +250,10 @@ define(['jquery', 'moment'], function ($, moment) {
                }
 
                $.each(rules, function(k, rule) {
+                       if (markParent) {
+                               // abort any further validation as validating the field already failed
+                               return false;
+                       }
                        switch (rule.type) {
                                case 'required':
                                        if (value === '') {
@@ -413,12 +417,10 @@ define(['jquery', 'moment'], function ($, moment) {
                        case 'is_in':
                                if (config.is_in) {
                                        theValue = '' + value;
-                                       for (a = 0; a < theValue.length; a++) {
-                                               var theChar = theValue.substr(a, 1);
-                                               if (config.is_in.indexOf(theChar) != -1) {
-                                                       newString += theChar;
-                                               }
-                                       }
+                                       // Escape special characters, see https://stackoverflow.com/a/6969486/4828813
+                                       config.is_in = config.is_in.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
+                                       var re = new RegExp('[^' + config.is_in + ']+', 'g');
+                                       newString = theValue.replace(re, '');
                                } else {
                                        newString = theValue;
                                }
@@ -604,6 +606,7 @@ define(['jquery', 'moment'], function ($, moment) {
        };
 
        /**
+        * Trims leading whitespace characters
         *
         * @param {String} value
         * @returns {String}
@@ -617,6 +620,7 @@ define(['jquery', 'moment'], function ($, moment) {
        };
 
        /**
+        * Trims trailing whitespace characters
         *
         * @param {String} value
         * @returns {String}