New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'search' constraint for sap.ui.model.type.String causing SyntaxError in XML view #971

Open
sdd64 opened this Issue Apr 27, 2016 · 10 comments

Comments

Projects
None yet
4 participants
@sdd64

sdd64 commented Apr 27, 2016

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}$'                 }               }"}
@ThomasChadzelek

This comment has been minimized.

Member

ThomasChadzelek commented Apr 27, 2016

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

@sdd64

This comment has been minimized.

sdd64 commented Apr 27, 2016

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?

@ThomasChadzelek

This comment has been minimized.

Member

ThomasChadzelek commented Apr 27, 2016

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?

@sdd64

This comment has been minimized.

sdd64 commented Apr 27, 2016

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 ^^

@sdd64

This comment has been minimized.

sdd64 commented Apr 27, 2016

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?

@ThomasChadzelek

This comment has been minimized.

Member

ThomasChadzelek commented Apr 27, 2016

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?

@sdd64

This comment has been minimized.

sdd64 commented Apr 27, 2016

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.

@ThomasChadzelek

This comment has been minimized.

Member

ThomasChadzelek commented Apr 27, 2016

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.

@codeworrior

This comment has been minimized.

Contributor

codeworrior commented Apr 27, 2016

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.

@Shtilianova

This comment has been minimized.

Contributor

Shtilianova commented Apr 12, 2017

I've created a internal incident 1770165494. You can check its status on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment