openui5
openui5 copied to clipboard
'search' constraint for sap.ui.model.type.String causing SyntaxError in XML view
OpenUI5 version: 1.28
I want to use a search constraint to valuate an user input. It is meant for a sap.ui.model.type.String type. The regex I want to use has been tested in online tools and works for controller based validation approaches (e.g. change).
I am not sure what is causing the SyntaxError. Is it 'search' or the regex?
<Label required="true" text="Staff - search"/>
<Input
placeholder="AB-12345-C"
value="{ path: 'StaffNr',
type : 'sap.ui.model.type.String',
constraints: {
search: '^[a-zA-Z]{2}-\d{5}-[a-zA-Z]{1}$'
}
}"/>
Object {name: "SyntaxError", message: "Bad string", at: 142, text: "{ path: 'Meins', type : 'sap.ui.m…}-[a-zA-Z]{1}$' } }"}
Hello sdd64!
Inside a complex binding expression, you need to take care of escaping as follows:
- Backslash and curly brace needs to be escaped (see BindingParser.complexParser.escape):
^[a-zA-Z]\{2\}-\\d\{5\}-[a-zA-Z]\{1\}$ - Inside a string literal, the corresponding quotes would need to be backslash escaped as well (single quotes in your example, but they do not appear inside the reg exp), see jQuery.sap.parseJS.
Best regards, Thomas
Hello Thomas,
thank you very much for this insight. Sadly, I still cant my regex to work. I parsed it through an escaper function, but the form still fails:
constraints: {
search: '\^\[a\-zA\-Z\]\{2\}\-d\{5\}\-\[a\-zA\-Z\]\{1\}\$'
}
What am I missing?
Not sure if you escaped too much. I just now see that my recommendation has been spoilt, guess I need to use proper formatting here or backslashes are lost. Let me see...
constraints: {
search: '^[a-zA-Z]\{2\}-\\d\{5\}-[a-zA-Z]\{1\}$'
}
What is the exact result you get? Any error message?
I am sorry Thomas, I should have mentioned that I already tried a regex based on your description. The one I created looked like yours above.
My excapt result is this:
Uncaught (in promise) Object {name: "SyntaxError", message: "Bad string", at: 142, text: "{ path : 'Meins', type : 'sap.ui.…[a-zA-Z]{1}$' } }"}at: 142message: "Bad string"name: "SyntaxError"text: "{ path : 'Meins', type : 'sap.ui.model.type.String' , constraints : { search : '^[a-zA-Z]{2}-\d{5}-[a-zA-Z]{1}$' } }"proto: Object
Note: the part in search is escaped in the preview as well ^^
Okay, I got it;
constraints: {
search: '^[a-zA-Z]{2}-\\d{5}-[a-zA-Z]{1}$'
}
Just had to escape the escape ~ Thank you, for your support.
Maybe this should be linked in the documentation?
Good point. Any suggestion where you would have looked for it? Which parts of the documentation did you already consult when you researched this issue?
Declarative Support: Data Binding looks like a good candidate to me. What do you think?
I would suggest to place it here, since sap.ui.model.type.String is currently the only type using the 'search' constraint.
What actualy helped me, besides your answers, was this previous issue about something else here. It is the only example about 'search' I was able to find.
The mentioned example says search: '^(?=[\\w\\d]{5,}$)' It just doubles the backslashes and does not escape the curly braces inside the string literal. Probably, I was too cautious and this is not needed. We should double check when we document it.
The top level parser for binding strings (base.BindingParser) calls the parseJS when it detects a binding object. So inside a binding object you don't need to escape curly braces, only the Javascript escaping is necessary. When curly braces occur top level in the binding string (outside a binding object), they need to be escaped.
Might be a bit hard to understand and was mainly driven by the implementation, but at the end it reduces the necessary escaping.
Last but not least, things get worse in an XMLView as the embedding of the binding string in an XML attribute might require additional escaping (XML entities). That's a post processing (for the encoder) resp. a preprocessing for a decoder. Means: when writing a binding string in an XMLView, first follow the above rules for escaping a binding strings, then apply the rules for XML escaping to the result.
I've created a internal incident 1770165494. You can check its status on GitHub.
I have created backlog item CPOUI5MODELS-920 for tracking.