Create a form in template redirecting to url containing the values of the form

I created a form that work fine.

Upon validation, it just redirect the visitor to an url that is customized based on data entered by user in the form.
This is the bottom part of the form in the frontmatter:

    buttons:
        -
            type: submit
            classes: 'button buttonlight'
            value: ' '
    process:
        -
            redirect: 'https://www.example.com?arrival={{ form.value.date_arrival|e }}&departure={{ form.value.date_departure|e }}&parameter_1={{ parameter_1 }}&parameter_2={{ parameter_2 }}'

Visitors are then sent to https://www.example.com?arrival=2022-06-22&departure=2022-06-23&parameter_1&parameter_2
Dates of that url are those entered by the visitor in the fields date_arrival and date_departure of the form.

Because I will end up having many of these forms, because these forms are almost all identical except parameters 1 & 2 that will be in each page’s frontmatter, I am considering creating the form in my_form_template.html.twig template, instead of the page’s frontmatter.
Should I require to modify the forms, this will allow me to do it in the template, once for all forms, instead of modifying all the pages one by one.

I tried the beginning of my form as:

<form name="form_no_20" action="https://www.example.com?arrival={{ form.value.date_arrival|e }}&departure={{ form.value.date_departure|e }}&parameter_1={{ parameter_1 }}&parameter_2={{ parameter_2 }}" method="POST">

The validation of the form redirects but the url does not include the data entered by the visitor.

I tried setting {% set date_arrival = form.value.date_arrival %} and replacing {{ form.value.date_arrival|e }} by {{ date_arrival }} in the url, but no change.

Am I trying to do something that I cannot do without some php/js (not within my skills) or am I just missing something?

Thank you for your help.

@red, Twig is being executed when a page has been requested. In other words, when the HTML (and in your case the form) is being generated. Hence the values entered by the user will not be available when Twig is being executed.

However, the values entered into form fields are available on the server after the form has been submitted. You could therefor…

  • Create a plugin.
  • Subscribe to and handle event onFormProcessed.
  • Extract the submitted values from the Form object which is passed into the eventhandler.
  • Redirect to other page with correct parameters.

Alternatively you could use JavaScript to extract values from the fields.

Thank you for your fast answer again and explaining.
I clearly lack some basic understanding of how it all works, but let’s keep learning.
I hope your answer will help others, or me at a later date.