Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Special characters in keys of a page.header.form

Solved by pamtbaau View solution

Started by Thomas 4 years ago · 6 replies · 428 views
4 years ago

Bonjour all,

I am creating a form that works as desired, but I wonder whether having the keys starting with a "&" might pose problems at some level (Grav, different browsers/environments…).

The form is redirecting to an url that includes data entered in the form by the visitor through a

YAML
            name: sort_choice
            type: radio
            label: 'Sort & filter'
            default: markdown
            options:
                &sort=distance: 'Nearest'
                &filter=date_lbl%253D90: 'Date'

(note that the keys start with "&")

and the form action

TWIG
redirect: 'https://example.com?test={{ form.value.sort_choice|e }}'

I wish the resulting url to be:

TXT
https://example.com?test=&sort=distance

but it comes out as:

TXT
https://example.com?test=&sort=distance

I also tested escaping the "&" in the Yaml with percent encoding "%26":

TXT
            options:
                %26sort=distance: 'Nearest'
                %26filter=date_lbl%253D90: 'Date'

and the url comes out as:

TXT
https://example.com?test=%26sort=distance

Both work fine on my local site but it feels weird… Is this safe in all environments to have a "&" in the key?

Tks.

4 years ago

Having such field names is so wrong.. 🤯
I'm not even sure what to suggest here, but one thing I'm certain about - you definitely need to change that logic and change how form is processed on the submit endpoint

👍 1
4 years ago

You confirm my feeling…
I checked in grav/admin/tools/logs and there is no error on this.
Before redirection, the form is correctly saving the form content in my user/data folder and it contains the field content as shown in the url. So all works as intended on my local Mamp installation.

Is this going to break when in production, wreak havoc in my hosting company’s infrastructure??

Can the key be escaped or encoded to safen the process(es)?

4 years ago

As seen on /forum/themes-styling/processing-twig-in-page-frontmatter-t4380, I tested:

TXT
options:
                '&sort=distance': 'Nearest'
                '&filter=date_lbl%253D90': 'Date'

It works as good. Would this be acceptable?

4 years ago

I honestly don't know what implications it might have on different server configurations. Probably none, but this is still so wrong.

May I ask what's the use case of such names? Why can't it be something like this?

YAML
            options:
                sort-distance: 'Nearest'
                filter-date-lbl-3D90: 'Date'
4 years ago Solution

@red,

The filter |e tells Twig to escape the string before outputting it (btw. escaping is already the default behaviour).

Hence your result:

TXT
https://example.com?test=&sort=distance

When using:

TWIG
redirect: https://example.com?test={{ form.value.sort_choice | raw }}

The result is:

TXT
https://example.com/?test=&sort=distance

Note:

  • The url you are fabricating isn't correct. It should be
    image|689x66

    See MDN on Url and section Parameters

4 years ago

Thank you both for your answers.
@pamtbaau I simplified the url, a little too much 😊, for the example. I am actually using an url as given by an external website which is formated as MDN.
I changed to |raw and result is as you explained.
I am keeping the keys as '&sort=distance'.

@Karmalakas The names I am using for the keys are the one I need to add inside the url of the external redirect website. When visitors select the "date" option, this will add &filter=date_lbl%253D90 inside the redirect url. This parameter is decided by the external website and twig operations cannot be done inside the redirect url. Visitors will arrive on the external website with some sorting and filtering of results already done.
I will check if the special characters will still work in production.

last edited 07/28/22 by Thomas

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 80 9 hours ago
General · by pamtbaau, 14 hours ago
1 51 14 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago