Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
Hi there!

I'm not great at regex, but I understand the basics. I have used regex101.com to check that what I am doing is correct, and as far as I can tell it is. I cannot for the life of me get it to work though.

On each templated out page of the site there is a custom field called "process_flowchart". This is set as an editable region type="file" but, under certain circumstances, the correct thing for a site editor to do is instead link directly to either a .pdf or to a different page of the website altogether (the latter being a new requirement!).

I got the .pdf part of this to work, using regex, some time ago:

Code: Select all
<cms:if "<cms:validate value=process_flowchart validator='regex=/pdf$/' />" >
    <cms:redirect url="<cms:show process_flowchart />#toolbar=0" />
<cms:else />

This simply opens up the .pdf file in the browser window - nice and easy!

I am now trying to sort out the 'redirect to a different page of the website' and wanted the regex to be fairly strict (i.e. not possible to link people to external sites). Here is what I tried:

Code: Select all
<cms:if "<cms:validate value=process_flowchart validator='regex=/((handbook.+)(\.php\?p\=)\d+$)/' />" >
    <cms:redirect url="<cms:show process_flowchart />" />
<cms:else />

As far as I can tell, and checking with regex101 seems to confirm this, this regex should match any string which contains "handbook" followed by any other characters, but ending with ".php?p=" followed by one or more digits. This would mean that the only URLs that would work would be ones along the lines of "http://handbook/trade-marks-eu.php?p=96" I checked again, and regex101 says that this URL would match the above regex test (you can see my regex101 testing here: https://regex101.com/r/dqFt8M/1)

And yet it doesn't work. Now, at first I was worried that this was because unlike the .html and .pdf situations, this one is not a 'file' so maybe something inside Couch was saying "nope - not a 'type=file' so I will not output this!", so I did a cms:dump_all to double check and it shows:
Code: Select all
process_flowchart: http://handbook/trade-marks-eu.php?p=96

to my mind, this should mean that the <cms:validate /> should work, as should the redirect, but it does not and it instead shows the error message that I set under a <cms:else />
I would've restricted Admin to enter certain content in separate dedicated fields - 3 inputs for pdfs other files and links. This way there is no need to do regular expressions.
I noted that link is taken in its basic form and if pasted in the pretty-url form the validation will fail. Try to find a better variant. Just a thought - maybe not to allow paste/type anything as a link and allow to select a page from a list - to achieve the best user experience. I always thought about selecting other pages, but never had a chance to create a working ui for this use case. Could be a nice addition to Couch, though.
Hi trendoman,

Thanks, but your solution would require quite a bit of extra work, since I would need to code the pages to only accept one of those 3 things - not something I have ever done before. I could use radio buttons so that the editor can declare what type of item it is, but this is not a good option either for the users we have.

I just would really like to know why that regex is failing... I have the following code:

Code: Select all
<cms:if "<cms:validate value=process_flowchart validator='regex=/((\/handbook.+\.)(php\?p\=)\d+$)/' />" >
    <cms:redirect url="<cms:show process_flowchart />" />
<cms:else />
<cms:if "<cms:validate value=process_flowchart validator='regex=/((\/handbook\/couch\/uploads\/file.+\.)(pdf$))/' />" >
    <cms:redirect url="<cms:show process_flowchart />#toolbar=0" />
<cms:else />
<cms:if "<cms:validate value=process_flowchart validator='regex=/((\/handbook\/couch\/uploads\/file.+\.)(html$))/' />" >
    <div class="container">
        ...content for the page...
    </div>
<cms:else />
    <div class="container">
        ...error message...
    </div>
</cms:if>
</cms:if>
</cms:if>


The latter two regex work - why not the first? If I enter a "http://handbook/...php?p=23" link into the custom field "process_flowchart" I get the error message - why? It should pass the regex, same as the pdf and html versions.
The answer is in how Couch reads the regex string. In short, you should also explicitly provide following parameters to avoid situations where certain important symbols are repeated in regex *not* being what Couch thinks of them.

separator
val_separator
case_sensitive

Precisely `equals` sign is the problem -
regex=/((\/handbook.+\.)(php\?p\=)\d+$)/
I see. Okay. I had thought that couch would have been coded to read anything between those / / as being the regex, regardless of what else couch thinks of items. I had used \ to escape the = character, so it is frustrating that this seems to be ignored by couch.

So which is it? I need to include \= in the regex string, so what do I need to tell couch in order to allow this to parse correctly? The documentation on 'separator' and 'val_separator' is scarce. :cry:
It's okay - trial and error got me there!! Thank you Trendoman!!

Code: Select all
<cms:if "<cms:validate value=process_flowchart validator='regex:/(\/handbook.+\.)(php\?p\=)\d+$/' val_separator=':' />" >
6 posts Page 1 of 1