Form: dynamic form action URL

Hello
I need to dynamically set the action URL of the form.

  1. Can I set to insert query string or uri.params automatically?
  2. I tried twig in action in the form definition but it will not be done
  3. I try to find a specific template and a place in it where you could override, but so far nothing works (there would be nice to add uri.params or anchor)
  4. Is it possible to change the action dynamically in the plugin during a custom process action (onFormProcessed)?

Thanks!

ad 3) Here you can use a redirect with the specified form value. https://learn.getgrav.org/16/forms/forms/reference-form-actions#redirect
In the template then something like this:

{% extends 'forms/form.html.twig' %}
{% block field %}
  {# next (redirect url) field #}
  {% if field.name == "next" %}
    {% set field = field|merge({'default': "redirect/url/"}) %}
  {% endif %}
{% endblock %}

But beware! If you want to use it like me to redirect to the same page, including uri.params, be sure to remove the “reset” action because it will clear the form data (including, of course, the field that contains the redirection target). Logical, but it took me some time :slight_smile:

@fosil, I’ve been playing a bit with forms…

  • In ‘user/plugins/form/templates/forms/default/form.html.twig’, on line 103, the action is defined in the following block:
    {% block embed_form_core %}
      name="{{ form.name }}"
      action="{{ action | trim('/', 'right') }}"
      method="{{ method }}"{{ multipart }}
      {% if form.id %}id="{{ form.id }}"{% endif %}
      {% if form.novalidate %}novalidate{% endif %}
      {% if form.keep_alive %}data-grav-keepalive="true"{% endif %}
    {% endblock %}
    
  • In Twig you can access ‘uri.query’ which returns for the url ‘http://localhost/grav/site-dev/typography?greeting=hello’ the value ‘greeting=hello’
  • When you copy ‘user/plugins/form/templates/forms/default/form.html.twig’ into your own theme, you might be able to add some logic using ‘uri.query’ and alter the action attribute to your liking.

Maybe this could lead to a solution…

Yes, that’s the solution. I tried in my template (extended from forms / form.html.twig) just overide the embed_form_core block (that would be much more elegant than having the entire code of that original template), but it didn’t work for me. Probably because it’s embedded in the “embed” tag.

So in the end I used a solution with override block “field” - I think it is clearer because it is clear what exactly had to be adjusted for a specific modification of functionality - and that will be a few months in looking into the code a great advantage :slight_smile:

So unfortunately … It didn’t turn out to be the ideal solution. Finally, I really overloaded the template form.html.twig. At the same time, I wrote a issue on the form plugin github to respect the value of the action variable if specified somewhere earlier - this is probably the ideal and most versatile solution.