Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Sightly check the url validation using sling model

savi50
Level 2
Level 2

Hi,

    I have written some  logic in the slingmodel and also i tried using sightly but due to syntax issue might be it is not working. I have tried 2 ways but it is not working.

problem statment: if the url is internal then we need to add extension as html and if url is external we do not add any extension.

                             

@Inject @Optional
private String path;
public boolean isexternalurl(){
return StringUtils.startsWith(path,"/content");

}
Sightly :
option 1:
<div data-sly-set.pathval="${model.path @extension='html'}">
</div>
<a href="${model.Isexternalurl ?'pathval':'model.path'}">
</a>

option 2:
<div data-sly-set.pathval="${model.path @extension='html'}">
</div>
<p><a href="${'/content/'in model.path ?'pathval':'model.path'}">Visit W3Schools.com!</a></p>
 

 

1 Accepted Solution
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi,

Try to get url from backend

 

example

/**
     * @param string url
     * @return string Formatted url
     */
    public static String getLink(String url) {
        String link = url;
        if (url != null) {
            final String regex = "(http:\\/\\/|https:\\/\\/(www)?)([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?";
            final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
            final Matcher matcher = pattern.matcher(url);
            if (!(matcher.find() || url.startsWith("#") || url.equals("/") || url.contains("?")
                            || url.toLowerCase()
                                            .matches(".*\\.(doc|docx|htm|html|odt|pdf|xls|xlsx|ods|ppt|pptx|txt|png|jpeg|jpg|xls|xlsx|svg|zip|shtml|json|log)$")
                            || url.toLowerCase().startsWith("tel:") || url.toLowerCase().startsWith("mailto:"))) {
                link = link + ".html";
            }
        }
        return link;
    }

View solution in original post

4 Replies
SivaPrasadReddyS
Level 8
Level 8

Hi @savi50 

 

Instead of having condition at sightly, you can write the logic at sling Model itself and just read the value in sightly.

you can try below and see if that works.

 

Sling Model:

 

@Inject @Optional

private String  path;

 

public void setPath() {

path = if(path.startsWith("/content”))? path +”html” : path ; 

}

public String getPath() {

return path; 

}

 

Sightly: 

<a href="${model.path'}"> Link Text
</a>

 

<div

ManuMathew1994
Level 4
Level 4

@savi50 You seems to be using single quotes for variables, it might be taking it as string instead of a variable?

BrianKasingli
Community Advisor
Community Advisor

I suggest you to write all the business logic in the sling model itself. So that you would only need to call the getter from your Sling Model, and the transformed url will be the final string that you will be using for the Url output.

Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi,

Try to get url from backend

 

example

/**
     * @param string url
     * @return string Formatted url
     */
    public static String getLink(String url) {
        String link = url;
        if (url != null) {
            final String regex = "(http:\\/\\/|https:\\/\\/(www)?)([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)?";
            final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
            final Matcher matcher = pattern.matcher(url);
            if (!(matcher.find() || url.startsWith("#") || url.equals("/") || url.contains("?")
                            || url.toLowerCase()
                                            .matches(".*\\.(doc|docx|htm|html|odt|pdf|xls|xlsx|ods|ppt|pptx|txt|png|jpeg|jpg|xls|xlsx|svg|zip|shtml|json|log)$")
                            || url.toLowerCase().startsWith("tel:") || url.toLowerCase().startsWith("mailto:"))) {
                link = link + ".html";
            }
        }
        return link;
    }

View solution in original post