config.xml extjs options maskRe und regex

hat jemand die extjs optionen maskRe oder regex erfolgreich in shopware config.xml benutzt?

laut Plugin configuration können wir extjs options addieren, also hab ich folgendes versucht:

    foo
    Foo
    0=69;400=49;1000=29
    
        /[0-9;=]/
        /^[0-9;=]+$/
        does this work?

aber es macht nicht was es - soweit ich verstanden habe - soll:

  • ich kann immer noch buchstaben eingeben, obwohl Ext JS 4.1.1 - Sencha Docs sagt es soll nicht passendes weg filtern
  • ich kann mit buchstaben speichern, obwohl Ext JS 4.1.1 - Sencha Docs sagt es soll nur speichern wenn diese regex passt. (naja, es sagt nicht das es speichern verhindert, sondern setzt auf invalid. aber invalid speichern wäre idiotisch)

und was müssen wir überhaupt machen um die änderungen in plugin in shopware-backend-frontend sichtbar zu machen? (ich mach: rm -rf var/cache/prod* und öffne die plugin einstellungen erneut.)

ps. dies ist mit shopware 5.4.6…

ich glaub ich hab die fehler gefunden.

in browser console kommt fehler:

TypeError: this.maskRe.test is not a function

also die string /[0-9;=]/ wird nicht als javascript regexp benutzt?

und wenn ich die options von shopware/Shopware.form.PluginPanel.js at 5.5 · shopware/shopware · GitHub ausgebe, bekomme ich:

 

Object { maskRe: "/[0-9;=]/", regex: "/^[0-9;=]+$/", regexText: "does this work?" }

 

jup, die regexp ist als string drin, nicht als regexp. hmm…

irgendwo müsste gesagt werden das dies ist keine string, sondern javascript regular expression, und sollte 1-zu-1 so gelassen werden…

ps. omg this forum syntax and editor is as horrible as 1999 was…

ich vermute dank https://github.com/shopware/shopware/blob/5.5/engine/Shopware/Components/Plugin/XmlConfigDefinitionReader.php#L168 werden wir nie javascript regular expression object benutzen können, weil es versucht die werte in php types zu konvertieren, und in php gibts keine regexp object, also wird es als string benutzt...

folgende könnte die einfachste workaround sein:
```
diff --git a/themes/Backend/ExtJs/backend/base/component/Shopware.form.PluginPanel.js b/themes/Backend/ExtJs/backend/base/component/Shopware.form.PluginPanel.js
index f419c793..62ed8791 100644
--- a/themes/Backend/ExtJs/backend/base/component/Shopware.form.PluginPanel.js
+++ b/themes/Backend/ExtJs/backend/base/component/Shopware.form.PluginPanel.js
@@ -244,6 +244,10 @@ Ext.define('Shopware.form.PluginPanel',
                 options = options || {};
                 delete options.attributes;

+ if (options["maskRe"]) options["maskRe"] = new RegExp(options["maskRe"]);
+ if (options["regex"]) options["regex"] = new RegExp(options["regex"]);
+ if (options["stripCharsRe "]) options["stripCharsRe "] = new RegExp(options["stripCharsRe "]);
+
                 elementName = element.get('name');
                 elementLabel = element.get('label');
                 elementDescription = element.get('description');
```

plus ne hinweis in dokumentation das die wert für diese in `config.xml` sollte nur die expression sein. also ohne "/" am anfang oder ende... und ja, bleibt die frage was macht man mit regexp flags... ich brauch aber momentan keine ;P

aber obwohl die regexp jetzt in frontend funktionieren, und auch wenn die eingaben korrekt sind, sagt shopware trotzdem: "Formular „FooBar“ konnte nicht gespeichert werden"...

auch mit folgende regel kann shopware nicht speichern:
```

	.*

```

und `maskRe` sollte eigentlich nichts mit speichert zu tun haben...

ich schau mal nächste woche weiter, oder so...

Erstell sonst gern ein PR auf Github. Ansonsten würde ich in der if abfrage eher Ext.isDefined benutzen :slight_smile:

TL;DR: die regexp werden korrekt in frontend benutzt, aber die plugin-config-werte werden nicht in backend gespeichert. und ich bin zu dumm die problem quelle zu finden, also gebe ich auf.

aber zumindest hab ich die genaue problem gefunden. (sorry für englisch, bin zu faul meine notizen zu übersetzen)...

new patch:
```
diff --git a/themes/Backend/ExtJs/backend/base/component/Shopware.form.PluginPanel.js b/themes/Backend/ExtJs/backend/base/component/Shopware.form.PluginPanel.js
index f419c79..f268c12 100644
--- a/themes/Backend/ExtJs/backend/base/component/Shopware.form.PluginPanel.js
+++ b/themes/Backend/ExtJs/backend/base/component/Shopware.form.PluginPanel.js
@@ -257,6 +257,17 @@ Ext.define('Shopware.form.PluginPanel',
                     }
                 }

+ // TODO: see if other xtypes have more regexps, these are from xtype text.
+ [
+ "maskRe",
+ "regex",
+ "stripCharsRe"
+ ].forEach(function(opt){
+ if (Ext.isDefined(options[opt])) {
+ options[opt] = new RegExp(options[opt]);
+ }
+ });
+
                 var field = Ext.apply({
                     xtype: type,
                     name: name,
```

plugin Resources/config.xml:
```


	
		
			foo_1
			maskRe [a-z]
			
				[a-z]
			
		
		
			foo_2
			regex ^[a-z]*$
			
				^[a-z]*$
			
		
	

```

works: heres what get posted to https://shopware.vanilla/backend/config/saveForm when we remove all regex options from config.xml:

{"id":279,"label":"FoobarRegexConfig","name":"FoobarRegexConfig","description":"test regex in config.xml","elements":[{"id":1064,"name":"foo_1","value":null,"label":"maskRe [a-z]","description":null,"type":"text","required":false,"scope":0,"options":[],"values":[{"id":null,"shopId":1,"value":"asd"}]},{"id":1065,"name":"foo_2","value":null,"label":"regex ^[a-z]*$","description":null,"type":"text","required":false,"scope":0,"options":[],"values":[{"id":null,"shopId":1,"value":"asd"}]}]}

fails: and heres with regexp options:

{"id":279,"label":"FoobarRegexConfig","name":"FoobarRegexConfig","description":"test regex in config.xml","elements":[{"id":1064,"name":"foo_1","value":null,"label":"maskRe [a-z]","description":null,"type":"text","required":false,"scope":0,"options":{"maskRe":undefined},"values":[{"id":null,"shopId":1,"value":"asd"}]},{"id":1065,"name":"foo_2","value":null,"label":"regex ^[a-z]*$","description":null,"type":"text","required":false,"scope":0,"options":{"regex":undefined},"values":[{"id":null,"shopId":1,"value":"asd"}]}]}

the differences are, in working one:

"options":[]

in failing one:

"options":{"maskRe":undefined}

and the server error is:

[Mon May 13 11:11:59.066325 2019] [:error] [pid 2886] [client 10.20.30.1:50436] PHP Fatal error: Uncaught Zend_Json_Exception: Decoding failed: Syntax error in /home/vagrant/web/shopware.vanilla/engine/Library/Zend/Json.php:63\nStack trace:\n#0 /home/vagrant/web/shopware.vanilla/engine/Library/Enlight/Controller/Plugins/JsonRequest/Bootstrap.php(93): Zend_Json::decode('{"id":279,"labe...')\n#1 /home/vagrant/web/shopware.vanilla/engine/Library/Enlight/Event/Handler/Default.php(91): Enlight_Controller_Plugins_JsonRequest_Bootstrap->onPreDispatch(Object(Enlight_Controller_ActionEventArgs))\n#2 /home/vagrant/web/shopware.vanilla/engine/Library/Enlight/Event/EventManager.php(219): Enlight_Event_Handler_Default->execute(Object(Enlight_Controller_ActionEventArgs))\n#3 /home/vagrant/web/shopware.vanilla/engine/Library/Enlight/Controller/Action.php(170): Enlight_Event_EventManager->notify('Enlight_Control...', Object(Enlight_Controller_ActionEventArgs))\n#4 /home/vagrant/web/shopware.vanilla/engine/Library/Enlight/Controller/Dispatcher/Default.php(563): Enlight_Controller_Action->dispatch('errorAction')\n#5 /home/vagran in /home/vagrant/web/shopware.vanilla/engine/Library/Zend/Json.php on line 63, referer: https://shopware.vanilla/backend/

that is not really helpful, so lets test in browser:

JSON.parse('{"id":279,"label":"FoobarRegexConfig","name":"FoobarRegexConfig","description":"test regex in config.xml","elements":[{"id":1064,"name":"foo_1","value":null,"label":"maskRe [a-z]","description":null,"type":"text","required":false,"scope":0,"options":{"maskRe":undefined},"values":[{"id":null,"shopId":1,"value":"asd"}]},{"id":1065,"name":"foo_2","value":null,"label":"regex ^[a-z]*$","description":null,"type":"text","required":false,"scope":0,"options":{"regex":undefined},"values":[{"id":null,"shopId":1,"value":"asd"}]}]}');
SyntaxError: JSON.parse: unexpected character at line 1 column 261 of the JSON data

so the problem is exactly at:

..."options":{"maskRe":undefined}...
                       ^

so something changes the regexp object to undefined, when posted to backend!

> rant: why does frontend even send backend the options? and given that `null` is allowed in json, why is not `undefined` allowed?

my tests show that shopware frontend code has the options intact everywhere, but looks like im too stupid to understand extjs, so i couldnt even find where it gets and sends the data...

so, given that extjs in shopware is dead old, and future version of shopware probably even gets rid of extjs, i'm bailing out on this one.

ps. wtf of the day, in ext-all-debug.js: eval("var batch = 30803;");

 

found the bugger! (after way too many F11's in devtools)

extjs uses its own json-to-string methods, and their implementation may create non compliant json strings, by using value `undefined`... (well, atleast php and javascript say `undefined` value in json is syntax error, so...)

heres the problem part:
```
        isObject: (toString.call(null) === '[object Object]') ?
        function(value) {
            
            return value !== null && value !== undefined && toString.call(value) === '[object Object]' && value.ownerDocument === undefined;
        } :
        function(value) {
            return toString.call(value) === '[object Object]';
        },
```

and heres what firefox/chrome give for object `/^[a-z]*$/`:
```
toString.call(/^[a-z]*$/);
"[object RegExp]"
```

so instead of recognizing `/foo/` as an object, it fails all tests and sets `undefined` as the value.

i have no clue why they are doing all this (its so old that JSON.stringify didnt exist?), but as a quick workaround, no value in json should ever be `undefined`, so this would fix it:
```
diff --git a/engine/Library/ExtJs/ext-all-debug.js b/engine/Library/ExtJs/ext-all-debug.js
index 89defff..673ccc7 100644
--- a/engine/Library/ExtJs/ext-all-debug.js
+++ b/engine/Library/ExtJs/ext-all-debug.js
@@ -6201,7 +6201,7 @@ Ext.JSON = (new(function() {
         } else if (typeof o === "function") {
             return "null";
         }
- return 'undefined';
+ return 'null';
     },
     m = {
         "\b": '\\b',
```

i guess you wont take pull-requests for ext-all*.js ? ;P
so who wants to fix/update extjs? >.